Skip to content

Commit

Permalink
Merge pull request RIOT-OS#12216 from benpicco/cleanup-test_malloc
Browse files Browse the repository at this point in the history
tests/malloc: cleanup
  • Loading branch information
jcarrano authored Sep 13, 2019
2 parents ab8cdac + 769fe44 commit 75c47a8
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions tests/malloc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,40 @@
#include <stdlib.h>
#include <string.h>

#ifndef CHUNK_SIZE
#define CHUNK_SIZE 1024
#endif

struct node {
struct node *next;
void *ptr;
};

int total = 0;
static int total = 0;

void fill_memory(struct node *head)
static void fill_memory(struct node *head)
{
while (head && (head->ptr = malloc(CHUNK_SIZE))) {
printf("Allocated %d Bytes at 0x%p, total %d\n", CHUNK_SIZE, head->ptr, total += CHUNK_SIZE);
printf("Allocated %d Bytes at 0x%p, total %d\n",
CHUNK_SIZE, head->ptr, total += CHUNK_SIZE);
memset(head->ptr, '@', CHUNK_SIZE);
head = head->next = malloc(sizeof(struct node));
if (head) {
head->ptr = 0;
total += sizeof(struct node);
head->ptr = 0;
head->next = 0;
}
total += sizeof(struct node);
}
}

void free_memory(struct node *head)
static void free_memory(struct node *head)
{
struct node *old_head;

while (head) {
if (head->ptr) {
printf("Free %d Bytes at 0x%p, total %d\n", CHUNK_SIZE, head->ptr, total -= CHUNK_SIZE);
printf("Free %d Bytes at 0x%p, total %d\n",
CHUNK_SIZE, head->ptr, total -= CHUNK_SIZE);
free(head->ptr);
}

Expand Down

0 comments on commit 75c47a8

Please sign in to comment.