PatchworkOS
Loading...
Searching...
No Matches
free.c
Go to the documentation of this file.
1#include <stdlib.h>
2
3#include "common/heap.h"
4
5#ifdef __KERNEL__
6#include <kernel/log/panic.h>
7#else
8#include <stdio.h>
9#endif
10
11void free(void* ptr)
12{
13 if (ptr == NULL)
14 {
15 return;
16 }
17
19
21
22 if (block->magic != _HEAP_HEADER_MAGIC)
23 {
24#ifdef __KERNEL__
25 panic(NULL, "heap corruption detected in free()");
26#else
27 printf("heap corruption detected in free()\n");
28 abort();
29#endif
30 }
31
32 if (!(block->flags & _HEAP_ALLOCATED))
33 {
34#ifdef __KERNEL__
35 panic(NULL, "double free detected in free()");
36#else
37 printf("double free detected in free()\n");
38 abort();
39#endif
40 }
41
42 _heap_free(block);
44}
static fd_t data
Definition dwm.c:21
void free(void *ptr)
Definition free.c:11
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:362
void _heap_acquire(void)
Acquire the heap lock.
Definition heap.c:81
void _heap_free(_heap_header_t *block)
Frees a previously allocated heap block.
Definition heap.c:259
#define _HEAP_HEADER_MAGIC
Definition heap.h:36
void _heap_release(void)
Release the heap lock.
Definition heap.c:90
@ _HEAP_ALLOCATED
Block is allocated.
Definition heap.h:53
#define NULL
Pointer error value.
Definition NULL.h:23
#define CONTAINER_OF(ptr, type, member)
Container of macro.
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition printf.c:5
_PUBLIC _NORETURN void abort(void)
Definition abort.c:7
_heap_flags_t flags
Definition heap.h:69
uint32_t magic
Definition heap.h:68