So I'm trying to implement my personal MemSet
that will do the same as memset
but also:
Copy word size chunks when possible, instead of byte by byte.
Guarantee dest's alignment
Test for all alignment possibilities
So this is my code:
void *MemSet(void *dest, int c, size_t n)
{
unsigned char *runner = (unsigned char *)dest;
size_t i = 0;
unsigned char swap_word[sizeof(size_t)];
for (i = 0; i < sizeof(size_t); ++i)
{
swap_word[i] = (unsigned char)c;
}
if (NULL == dest)
{
return (NULL);
}
while (n > 0)
{
/* setting byte by byte */
if (n < sizeof(size_t) || (((size_t)runner & (sizeof(size_t) - 1)) != 0))
{
*runner++ = (unsigned char)c;
--n;
printf("Byte written\n"); /* for debugging */
}
else
{
/* setting a whole word */
*((void **)runner) = *((void **)swap_word);
runner += sizeof(size_t);
n -= sizeof(size_t);
printf("Word written\n"); /* for debugging */
}
}
return (dest);
}
What am I doing here?
creating an unsigned char pointer
runner
to run over thedest
but without changing it address so I'll be able to return it as a return value.creating a ready
swap_word
array, with the size ofsizeof(size_t)
because this size determines whether my machine is 32 or 64 bit (and therefore theWORD
size is 4 or 8 bytes. This array will swapped when I'll need to set a word.running a simple
while loop
that will check if there are more thansizeof(size_t)
bytes left to set , if not, it means for sure we won't be able to set a whole word and then set them byte by byte.Another option to set the bytes byte by byte is if the address isn't divides by 4 or 8 (again, depends on the machine), which means I won't be able to set up a word without crossing
WORD Boundary
, so just set them byte by byte until I'll reach an aligned address.Only option to set up a whole word is only if the data is already aligned to the
WORD size
of the machine, and then just set up 8 bytes (just set them up using the array ofswap_word
we made earlier, and advance 8 more addresses. I'll do it by using casting of*((void **)runner) = *((void **)swap_word);
and this is my test file:
int array[] = { 2, 3 };
int main ()
{
for (i = 0; i < 2; i++)
{
printf("Before MemSet, target is \"%d\"\n\n", array[i]);
}
if (NULL == MemSet(array, 3, 2 * sizeof(int)))
{
fprintf(stderr,"MemSet failed!\n");
}
for (i = 0; i < 2; i++)
{
printf("After MemSet, target is \"%d\"\n\n", array[i]);
}
return (0);
}
Output is:
Before Memset, target is "2"
Before Memset, target is "3"
Word written
After Memset, target is "50529027"
After Memset, target is "50529027"
Why aren't the elements are '3'? both of them? I'm using here
MemSet(array, 3, 2 * sizeof(int))
Which, by theory, needs to set up both of the elements as 3
because the array uses 2*sizeof(int)
spaces in the memory, and I set up all of them as 3
.
What do you think? And also, how can I check if my alignment works?
Thanks.