-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from 0152la/lua-testing2
Improve memory allocator
- Loading branch information
Showing
4 changed files
with
245 additions
and
25 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
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
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
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 |
---|---|---|
@@ -1,12 +1,80 @@ | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
static inline void | ||
check_next(void *addr, void *to_check) | ||
{ | ||
assert(*((void **) ((char *) addr - sizeof(void *))) == to_check); | ||
} | ||
|
||
int | ||
main(void) | ||
{ | ||
const char *hw = "Hello World!"; | ||
char *x = malloc(strlen(hw)); | ||
const size_t hw_sz = strlen(hw); | ||
|
||
char *x = malloc(hw_sz + 1); | ||
strcpy(x, hw); | ||
assert(!strcmp(x, "Hello World!")); | ||
|
||
x = realloc(x, hw_sz * 2 + 1); | ||
strcat(x, hw); | ||
assert(!strcmp(x, "Hello World!Hello World!")); | ||
|
||
x = realloc(x, hw_sz + 1); | ||
x[strlen(hw)] = '\0'; | ||
assert(!strcmp(x, "Hello World!")); | ||
|
||
free(x); | ||
|
||
const size_t malloc_block_sz = 16; | ||
|
||
// Check free | ||
void *tmp01 = malloc(2 * malloc_block_sz); | ||
void *tmp02 = malloc(1 * malloc_block_sz); | ||
free(tmp01); | ||
tmp01 = malloc(2 * malloc_block_sz); | ||
free(tmp02); | ||
free(tmp01); | ||
|
||
// Check realloc | ||
void *tmp11 = realloc(NULL, 2 * malloc_block_sz); | ||
void *tmp13 = realloc(NULL, 1 * malloc_block_sz); | ||
void *tmp12 = realloc(tmp11, 2 * malloc_block_sz); | ||
|
||
assert(tmp11 == tmp12); | ||
|
||
void *tmp14 = realloc(tmp12, 4 * malloc_block_sz); | ||
check_next(tmp13, tmp14); | ||
|
||
void *tmp15 = malloc(2 * malloc_block_sz); | ||
check_next(tmp15, tmp13); | ||
|
||
free(tmp13); | ||
check_next(tmp15, tmp14); | ||
free(tmp15); | ||
free(tmp14); | ||
|
||
// Check finding right block to insert | ||
void *tmp1 = malloc(1 * malloc_block_sz); | ||
void *y = malloc(3 * malloc_block_sz); | ||
void *tmp2 = malloc(1 * malloc_block_sz); | ||
void *tmp3 = malloc(5 * malloc_block_sz); | ||
free(y); | ||
void *tmp4 = malloc(malloc_block_sz); | ||
void *tmp5 = malloc(malloc_block_sz); | ||
|
||
assert(tmp1 < tmp4); | ||
assert(tmp1 < tmp5); | ||
assert(tmp4 < tmp2); | ||
assert(tmp5 < tmp2); | ||
|
||
free(tmp1); | ||
free(tmp2); | ||
free(tmp3); | ||
free(tmp4); | ||
free(tmp5); | ||
|
||
return 0; | ||
} |