2

I was introduced to a question as follows,

I need to implement this API:

a)Implement an allocation function: void *myMalloc(int size) , which gets int size of bytes to allocate , and returns a location in memory which is aligned to 16 bytes and these "size" bytes set to zero.

b) Implement a "deallocation" function: void myFree(void *ptr) which gets a pointer to a location in memory that has been allocated by the myMalloc() function , and frees that location.

I am working on Ubuntu 12.04 LTS, using gcc.

My implementations are bellow (with a sample main to test it).

I have tested the program with valgrind and got no errors --> "All heap blocks were freed -- no leaks are possible"

#include <stdio.h>
#include <stdlib.h>

void *myMalloc(int size)
{
    void *ret;
    char res;

    // 0) check valid argument
    if (0 >= size) 
        return NULL;

    // 1) allocate size bytes plus 16 extra for the alignment
    ret = malloc(sizeof(char) * size + 16);
    if (!ret)
        return NULL;

    // 2) calculate the residue of the address we got from malloc
    //    and forward the pointer ONE byte before an "aligned" address 
    res = (long)ret % 16;
    ret = (char *)ret + 16 - 1 - res;

    // 3) store the offset in that byte and then
    //    forward the return address ONE byte ahead 
    *(char *)ret++ = res;   // **NOTE B**

    // 4) set the (requested) "size" bytes to zero 
    char *tmp = (char *)ret;
    while (0 < size--)
        *tmp++ = 0;

    return ret;

} 


void myFree(void *ptr)
{
    char res = *((char *)ptr - 1);  // **NOTE C**
    ptr = (char *)ptr - res;
    free(ptr);  

} 

void main()
{

    void *p = myMalloc(17);
    printf("within main, p points to: %p \n",p);
    myFree(p);

} 

NOTE:

A) I stored the "offset" ONE byte before the address I returned to the main due to the fact that res WILL always be a number between [0 - 15] , therefor we dont need more than ONE byte to store it.

So my questions are:

1) Are there any improvements I can do in order to improve my API regarding to run time and/or memory allocations I have performed ?

2) Just to be sure , at NOTE B and NOTE C I have "casted" the pointer to char * "only" once , so that casting "gets into action" for the subtraction of the address and for the "deferenceing" needed to extract the value (char) which is located there ?

3) Are there any considerations I should be aware regrading the OS I an working on ? meaning , will it work differently , say , on Windows ?

Thanks allot in advance,

Guy.

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
  • 1
    There is really no need to re-invent the wheel - such APIs exist on all common platforms, e.g. http://man7.org/linux/man-pages/man3/posix_memalign.3.html on Linux. – Paul R Apr 03 '14 at 14:47
  • 1
    You've stated the specs. You've written the code. Test it against the specs. If it meets or exceeds each spec, it works, if not it needs work. Questions like _are there any improvements..._? or _Are there any considerations?_ are naturally going to invite people to share their opinions. Not really the focuses of this forum. – ryyker Apr 03 '14 at 15:00

0 Answers0