1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 06:23:53 +01:00

Merge pull request #12216 from benpicco/cleanup-test_malloc

tests/malloc: cleanup
This commit is contained in:
Juan I Carrano 2019-09-13 12:03:48 +02:00 committed by GitHub
commit 75c47a8dc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}