Skip to content

Commit

Permalink
implement the simple_malloc with sbrk sys call
Browse files Browse the repository at this point in the history
  • Loading branch information
YeonwooSung committed Sep 14, 2018
1 parent 98e047f commit 600c823
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
23 changes: 23 additions & 0 deletions simple_malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <assert.h>
#include <string.h>
#include <unistd.h>

#include "simple_malloc.h"

/**
* A simple implementation of memory allocating function.
*
* @param size the size of the memory that should be allocated.
* @return If the sbrk failed, returns NULL. Otherwise, returns p, which is the starting point of the allocated memory.
*/
void *simpleMalloc(size_t size) {
void *p = sbrk(0);
void *request = sbrk(size);

if (request == (void *)-1) {
return NULL; //sbrk failed
} else {
assert(p == request); //Not thread safe.
return p;
}
}
9 changes: 9 additions & 0 deletions simple_malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef SIMPLE_MALLOC_H
#define SIMPLE_MALLOC_H

#include <sys/types.h>

/* The simple implementation of memory allocating function. */
void *simple_malloc(size_t size);

#endif

0 comments on commit 600c823

Please sign in to comment.