free fixes, gonna do large memory zones next
This commit is contained in:
parent
1b8b22fbdf
commit
4482024705
6 changed files with 15 additions and 9 deletions
2
malloc/.gitignore
vendored
Normal file
2
malloc/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
myprogram
|
||||||
|
*.o
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
# define MALLOC_H
|
# define MALLOC_H
|
||||||
|
|
||||||
# define malloc_n 64
|
# define malloc_n 64
|
||||||
# define malloc_m 512
|
# define malloc_m 1024
|
||||||
# define malloc_N (1 * getpagesize())
|
# define malloc_N (1 * getpagesize())
|
||||||
# define malloc_M (2 * getpagesize())
|
# define malloc_M (2 * getpagesize())
|
||||||
# define malloc_magic 1234567
|
# define malloc_magic 1234567
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -6,11 +6,11 @@ int main(void)
|
||||||
printf("sizeof(long)=[%lu]\n", sizeof(long));
|
printf("sizeof(long)=[%lu]\n", sizeof(long));
|
||||||
printf("sizeof(t_node)=[%lu]\n", sizeof(t_node));
|
printf("sizeof(t_node)=[%lu]\n", sizeof(t_node));
|
||||||
|
|
||||||
void *ptr0 = malloc(16 * sizeof(long));
|
void *ptr0 = malloc(8190 * sizeof(long));
|
||||||
show_alloc_mem();
|
show_alloc_mem();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
void *ptr1 = malloc(32 * sizeof(long));
|
void *ptr1 = malloc(300 * sizeof(long));
|
||||||
show_alloc_mem();
|
show_alloc_mem();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
|
@ -22,9 +22,9 @@ int main(void)
|
||||||
/* show_alloc_mem(); */
|
/* show_alloc_mem(); */
|
||||||
/* printf("\n"); */
|
/* printf("\n"); */
|
||||||
|
|
||||||
free(ptr0);
|
/* free(ptr0); */
|
||||||
show_alloc_mem();
|
/* show_alloc_mem(); */
|
||||||
printf("\n");
|
/* printf("\n"); */
|
||||||
|
|
||||||
free(ptr1);
|
free(ptr1);
|
||||||
show_alloc_mem();
|
show_alloc_mem();
|
||||||
|
|
|
||||||
BIN
malloc/myprogram
BIN
malloc/myprogram
Binary file not shown.
|
|
@ -7,18 +7,22 @@ t_node *small_alloc = NULL;
|
||||||
|
|
||||||
t_node **find_node(t_node **node, size_t size)
|
t_node **find_node(t_node **node, size_t size)
|
||||||
{
|
{
|
||||||
while (*node && (size_t)(*node)->size < sizeof(t_node) + size)
|
while (*node && (size_t)(*node)->size < size + HEADER_SIZE)
|
||||||
node = &(*node)->next;
|
node = &(*node)->next;
|
||||||
return (node);
|
return (node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_chunk(t_node **node_ref, size_t size)
|
void add_chunk(t_node **node_ref, size_t size)
|
||||||
{
|
{
|
||||||
|
size_t chunk_size;
|
||||||
|
|
||||||
while (*node_ref)
|
while (*node_ref)
|
||||||
node_ref = &(*node_ref)->next;
|
node_ref = &(*node_ref)->next;
|
||||||
*node_ref = mmap(NULL, malloc_N, PROT_READ|PROT_WRITE,
|
chunk_size = TINY(size) ? malloc_N : malloc_M;
|
||||||
|
printf("chunk_size=[%zu]\n", chunk_size);
|
||||||
|
*node_ref = mmap(NULL, (chunk_size +1) * 8, PROT_READ|PROT_WRITE,
|
||||||
MAP_ANON|MAP_PRIVATE, -1, 0);
|
MAP_ANON|MAP_PRIVATE, -1, 0);
|
||||||
(*node_ref)->size = TINY(size) ? malloc_N : malloc_M;
|
(*node_ref)->size = chunk_size;
|
||||||
(*node_ref)->next = NULL;
|
(*node_ref)->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue