-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc_1.cpp
33 lines (28 loc) · 907 Bytes
/
malloc_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <unistd.h>
#define MAX_SMALLOC_SIZE 100000000 // 10^8
#define ZERO_SMALLOC 0
/**
void* smalloc(size_t size)
- Tries to allocate ‘size’ bytes.
@return:
Success:
a pointer to the first allocated byte within the allocated block.
Failure:
a. If ‘size’ is 0 returns NULL.
b. If ‘size’ is more than 10^8, return NULL.
c. If sbrk fails, return NULL.
*/
void* smalloc(size_t size){
if (size == ZERO_SMALLOC || size > MAX_SMALLOC_SIZE) {
return NULL;
}
// TODO: check if need to cast to (intptr_t), which is "long int"
long int sizeCast = (long int)size;
void* newBlock = sbrk(sizeCast);
if (newBlock == (void*)(-1)) { // sbrk fails
return NULL;
}
// sbrk returns the old program break,
// which is now the start of the newly allocated memory
return newBlock;
}