amath  1.8.5
Simple command line calculator
memset.c File Reference

Fill block of memory with a constant value. More...

#include "amathc.h"
Include dependency graph for memset.c:

Go to the source code of this file.

Typedefs

typedef uintptr_t mem_ptr
 

Functions

void MemSet (void *dst0, int c0, unsigned int length)
 Fill block of memory with a constant value. More...
 

Detailed Description

Fill block of memory with a constant value.

Code originate from FreeBSD base, revision 229286.

The original source code can be obtained from: https://svnweb.freebsd.org/base/head/lib/libc/string/memset.c?revision=229286

Definition in file memset.c.

Typedef Documentation

◆ mem_ptr

typedef uintptr_t mem_ptr

Definition at line 51 of file memset.c.

Function Documentation

◆ MemSet()

void MemSet ( void *  dst0,
int  c0,
unsigned int  length 
)

Fill block of memory with a constant value.

Definition at line 56 of file memset.c.

57 {
58  unsigned char* dst = (unsigned char*) dst0;
59  unsigned int t;
60  unsigned int c;
61 
62  /*
63  * If not enough words, just fill bytes. A length >= 2 words
64  * guarantees that at least one of them is `complete' after
65  * any necessary alignment. For instance:
66  *
67  * |-----------|-----------|-----------|
68  * |00|01|02|03|04|05|06|07|08|09|0A|00|
69  * ^---------------------^
70  * dst dst+length-1
71  *
72  * but we use a minimum of 3 here since the overhead of the code
73  * to do word writes is substantial.
74  */
75  if (length < 3 * wsize)
76  {
77  while (length != 0)
78  {
79  *dst++ = c0;
80  --length;
81  }
82  }
83 
84  if ((c = (unsigned char)c0) != 0)
85  { /* Fill the word. */
86  c = (c << 8) | c; /* u_int is 16 bits. */
87 #if UINT_MAX > 0xffff
88  c = (c << 16) | c; /* u_int is 32 bits. */
89 #endif
90 #if UINT_MAX > 0xffffffff
91  c = (c << 32) | c; /* u_int is 64 bits. */
92 #endif
93  }
94 
95  /* Align destination by filling in bytes. */
96  if ((t = (mem_ptr)dst & wmask) != 0)
97  {
98  t = wsize - t;
99  length -= t;
100  do
101  {
102  *dst++ = c0;
103  }
104  while (--t != 0);
105  }
106 
107  /* Fill words. Length was >= 2*words so we know t >= 1 here. */
108  t = length / wsize;
109  do
110  {
111  *(unsigned int*)dst = c;
112  dst += wsize;
113  }
114  while (--t != 0);
115 
116  /* Mop up trailing bytes, if any. */
117  t = length & wmask;
118  if (t != 0)
119  do
120  {
121  *dst++ = c0;
122  }
123  while (--t != 0);
124 }
#define wmask
Definition: amath.h:233
#define wsize
Definition: amath.h:232
uintptr_t mem_ptr
Definition: memcpy.c:60