Subversion Repositories Kolibri OS

Rev

Rev 4874 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4874 Rev 4921
1
/*
1
/*
2
FUNCTION
2
FUNCTION
3
	<>---set an area of memory
3
	<>---set an area of memory
4
 
4
 
5
INDEX
5
INDEX
6
	memset
6
	memset
7
 
7
 
8
ANSI_SYNOPSIS
8
ANSI_SYNOPSIS
9
	#include 
9
	#include 
10
	void *memset(void *<[dst]>, int <[c]>, size_t <[length]>);
10
	void *memset(void *<[dst]>, int <[c]>, size_t <[length]>);
11
 
11
 
12
TRAD_SYNOPSIS
12
TRAD_SYNOPSIS
13
	#include 
13
	#include 
14
	void *memset(<[dst]>, <[c]>, <[length]>)
14
	void *memset(<[dst]>, <[c]>, <[length]>)
15
	void *<[dst]>;
15
	void *<[dst]>;
16
	int <[c]>;
16
	int <[c]>;
17
	size_t <[length]>;
17
	size_t <[length]>;
18
 
18
 
19
DESCRIPTION
19
DESCRIPTION
20
	This function converts the argument <[c]> into an unsigned
20
	This function converts the argument <[c]> into an unsigned
21
	char and fills the first <[length]> characters of the array
21
	char and fills the first <[length]> characters of the array
22
	pointed to by <[dst]> to the value.
22
	pointed to by <[dst]> to the value.
23
 
23
 
24
RETURNS
24
RETURNS
25
	<> returns the value of <[dst]>.
25
	<> returns the value of <[dst]>.
26
 
26
 
27
PORTABILITY
27
PORTABILITY
28
<> is ANSI C.
28
<> is ANSI C.
29
 
29
 
30
    <> requires no supporting OS subroutines.
30
    <> requires no supporting OS subroutines.
31
 
31
 
32
QUICKREF
32
QUICKREF
33
	memset ansi pure
33
	memset ansi pure
34
*/
34
*/
35
 
35
 
36
#include 
36
#include 
-
 
37
#include "local.h"
37
 
38
 
38
#define LBLOCKSIZE (sizeof(long))
39
#define LBLOCKSIZE (sizeof(long))
39
#define UNALIGNED(X)   ((long)X & (LBLOCKSIZE - 1))
40
#define UNALIGNED(X)   ((long)X & (LBLOCKSIZE - 1))
40
#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
41
#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
41
 
42
 
42
_PTR
43
_PTR
-
 
44
__inhibit_loop_to_libcall
43
_DEFUN (memset, (m, c, n),
45
_DEFUN (memset, (m, c, n),
44
	_PTR m _AND
46
	_PTR m _AND
45
	int c _AND
47
	int c _AND
46
	size_t n)
48
	size_t n)
47
{
49
{
48
  char *s = (char *) m;
50
  char *s = (char *) m;
49
 
51
 
50
#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
52
#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
51
  int i;
53
  int i;
52
  unsigned long buffer;
54
  unsigned long buffer;
53
  unsigned long *aligned_addr;
55
  unsigned long *aligned_addr;
54
  unsigned int d = c & 0xff;	/* To avoid sign extension, copy C to an
56
  unsigned int d = c & 0xff;	/* To avoid sign extension, copy C to an
55
				   unsigned variable.  */
57
				   unsigned variable.  */
56
 
58
 
57
  while (UNALIGNED (s))
59
  while (UNALIGNED (s))
58
    {
60
    {
59
      if (n--)
61
      if (n--)
60
        *s++ = (char) c;
62
        *s++ = (char) c;
61
      else
63
      else
62
        return m;
64
        return m;
63
    }
65
    }
64
 
66
 
65
  if (!TOO_SMALL (n))
67
  if (!TOO_SMALL (n))
66
    {
68
    {
67
      /* If we get this far, we know that n is large and s is word-aligned. */
69
      /* If we get this far, we know that n is large and s is word-aligned. */
68
      aligned_addr = (unsigned long *) s;
70
      aligned_addr = (unsigned long *) s;
69
 
71
 
70
      /* Store D into each char sized location in BUFFER so that
72
      /* Store D into each char sized location in BUFFER so that
71
         we can set large blocks quickly.  */
73
         we can set large blocks quickly.  */
72
      buffer = (d << 8) | d;
74
      buffer = (d << 8) | d;
73
      buffer |= (buffer << 16);
75
      buffer |= (buffer << 16);
74
      for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
76
      for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
75
        buffer = (buffer << i) | buffer;
77
        buffer = (buffer << i) | buffer;
76
 
78
 
77
      /* Unroll the loop.  */
79
      /* Unroll the loop.  */
78
      while (n >= LBLOCKSIZE*4)
80
      while (n >= LBLOCKSIZE*4)
79
        {
81
        {
80
          *aligned_addr++ = buffer;
82
          *aligned_addr++ = buffer;
81
          *aligned_addr++ = buffer;
83
          *aligned_addr++ = buffer;
82
          *aligned_addr++ = buffer;
84
          *aligned_addr++ = buffer;
83
          *aligned_addr++ = buffer;
85
          *aligned_addr++ = buffer;
84
          n -= 4*LBLOCKSIZE;
86
          n -= 4*LBLOCKSIZE;
85
        }
87
        }
86
 
88
 
87
      while (n >= LBLOCKSIZE)
89
      while (n >= LBLOCKSIZE)
88
        {
90
        {
89
          *aligned_addr++ = buffer;
91
          *aligned_addr++ = buffer;
90
          n -= LBLOCKSIZE;
92
          n -= LBLOCKSIZE;
91
        }
93
        }
92
      /* Pick up the remainder with a bytewise loop.  */
94
      /* Pick up the remainder with a bytewise loop.  */
93
      s = (char*)aligned_addr;
95
      s = (char*)aligned_addr;
94
    }
96
    }
95
 
97
 
96
#endif /* not PREFER_SIZE_OVER_SPEED */
98
#endif /* not PREFER_SIZE_OVER_SPEED */
97
 
99
 
98
  while (n--)
100
  while (n--)
99
    *s++ = (char) c;
101
    *s++ = (char) c;
100
 
102
 
101
  return m;
103
  return m;
102
}
104
}