-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement the simple_malloc with sbrk sys call
- Loading branch information
1 parent
98e047f
commit 600c823
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |