Subversion Repositories freemyipod

Rev

Rev 424 | Details | Compare with Previous | Last modification | View Log | RSS feed

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