Skip to content

Commit

Permalink
gamp
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Sep 26, 2023
1 parent ce0a7c5 commit 9faa0e6
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,25 @@ void _free(void *ptr) {
map->prev->next = map->next;
if (map->next)
map->next->prev = map->prev;
if (map == g_mmap)
g_mmap = map->next;
// FIX: this cause page reclaims to skyrocket
/* if (map == g_mmap) */
/* g_mmap = map->next; */
t_mmap *gmmap = g_mmap;
(void)gmmap;
if (map == g_mmap) {
/* gmmap = map->next; */
if (map->next)
g_mmap = map->next;
else
g_mmap = NULL;
}
munmap(map, map->size + sizeof(t_mmap));
}
}

void free(void *ptr) {
(void)ptr;
/* return; */
if (LOGGING) {
ft_dprintf(tmpfd(), "free(%p)\n", ptr);
/* flog("free(): ", (size_t)ptr); */
Expand Down
5 changes: 3 additions & 2 deletions src/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ t_type get_mmap_type(size_t size) {
}

t_mmap *new_mmap(size_t size) {
t_mmap *map = mmap(NULL, get_mmap_size(size) + sizeof(t_mmap),
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
t_mmap *map =
mmap(NULL, get_mmap_size(size) + sizeof(t_mmap), PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (!map) {
errno = ENOMEM;
return NULL;
Expand Down
Binary file added test0
Binary file not shown.
14 changes: 14 additions & 0 deletions test0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdlib.h>

int main(void)
{
int i;
char *addr;

i = 0;
while (i < 1024)
{
i++;
}
return (0);
}
Binary file added test1
Binary file not shown.
28 changes: 28 additions & 0 deletions test1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void print(char *s)
{
write(1, s, strlen(s));
}

int main(void)
{
int i;
char *addr;

i = 0;
while (i < 1024)
{
addr = (char*)malloc(1024);
if (addr == NULL)
{
print("Failed to allocate memory\n");
return (1);
}
addr[0] = 42;
i++;
}
return (0);
}
Binary file added test2
Binary file not shown.
24 changes: 24 additions & 0 deletions test2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void print(char *s) { write(1, s, strlen(s)); }

void test(void *p);
int main(void) {
int i;
char *addr;

i = 0;
while (i < 1024) {
addr = (char *)malloc(1024);
if (addr == NULL) {
print("Failed to allocate memory\n");
return (1);
}
addr[0] = 42;
free(addr);
i++;
}
return (0);
}
Binary file added test3
Binary file not shown.
41 changes: 41 additions & 0 deletions test3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define M (1024 * 1024)

void print(char *s)
{
write(1, s, strlen(s));
}

int main()
{
char *addr1;
char *addr2;
char *addr3;

addr1 = (char*)malloc(16*M);
if (addr1 == NULL)
{
print("Failed to allocate memory\n");
exit(1);
}
strcpy(addr1, "Hello world!\n");
print(addr1);
addr2 = (char*)malloc(16*M);
if (addr2 == NULL)
{
print("Failed to allocate memory\n");
exit(1);
}
addr3 = (char*)realloc(addr1, 128*M);
if (addr3 == NULL)
{
print("Failed to reallocate memory\n");
exit(1);
}
addr3[127*M] = 42;
print(addr3);
return (0);
}
Binary file added test4
Binary file not shown.
26 changes: 26 additions & 0 deletions test4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdlib.h>

#define M (1024 * 1024)

int main()
{
malloc(1);
malloc(2);
malloc(4);
malloc(8);
malloc(16);
malloc(32);
malloc(64);
malloc(128);
malloc(256);
malloc(512);
malloc(1024);
malloc(1024 * 2);
malloc(1024 * 4);
malloc(1024 * 32);
malloc(M);
malloc(16*M);
malloc(128*M);
show_alloc_mem();
return (0);
}
Binary file added test5
Binary file not shown.
34 changes: 34 additions & 0 deletions test5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void print(char *s)
{
write(1, s, strlen(s));
}

int main()
{
int i;
int alignment;
char *addr;

i = 1;
alignment = 2 * sizeof(size_t);
while (i <= 100)
{
addr = (char*)malloc(i);
if (addr == NULL)
{
print("Failed to allocate memory\n");
exit(1);
}
if ((((unsigned long) (addr)) % alignment) != 0)
{
print("malloc returned a non aligned boundary\n");
exit(1);
}
i++;
free(addr);
}
}

0 comments on commit 9faa0e6

Please sign in to comment.