Derive-C
Loading...
Searching...
No Matches
std.h
Go to the documentation of this file.
1
2// and just uses malloc/calloc/free.
3
4#pragma once
5
6#include <stdio.h>
7#include <stdlib.h>
8
12
13DC_ZERO_SIZED(stdalloc);
14static stdalloc stdalloc_instance = {};
16
17DC_PUBLIC static void* NS(stdalloc, allocate_uninit)(stdalloc_ref /* ref */, size_t size) {
18 DC_ASSERT(size > 0, "Cannot allocate zero sized");
19 void* alloc = malloc(size);
20
21 // JUSTIFY: Setting memory capabilities
22 // - Flakiness observed for tests running under msan with uninstrumented glibc
24 DC_ASSERT(alloc != NULL, "Standard allocator failed to malloc");
25 return alloc;
26}
27
28DC_PUBLIC static void* NS(stdalloc, allocate_zeroed)(stdalloc_ref /* ref */, size_t size) {
29 DC_ASSERT(size > 0, "Cannot allocate zero sized");
30 void* alloc = calloc(size, 1);
31
32 // JUSTIFY: Setting memory capabilities
33 // - Flakiness observed for tests running under msan with uninstrumented glibc
35 size);
36
37 DC_ASSERT(alloc != NULL, "Standard allocator failed to calloc");
38 return alloc;
39}
40
41DC_PUBLIC static void* NS(stdalloc, reallocate)(stdalloc_ref /* ref */, void* ptr, size_t old_size,
42 size_t new_size) {
43 DC_ASSERT(new_size > 0, "Cannot allocate zero sized");
44 DC_ASSERT(ptr, "Cannot reallocate a null pointer");
45 DC_ASSUME(old_size > 0, "Could never have allocated zero sized");
46
47 if (new_size < old_size) {
48 // JUSTIFY: We poison the memory we will no longer use.
49 // - When reallocating by reducing allocation size, msan does not automatically poison the
50 // bytes that are no longer part of the allocation
51 // - We need this in order to pass alloc fuzz tests: we want the strong property that
52 // deallocate memory is poisoned.
54 (char const*)ptr + new_size, old_size - new_size);
55 }
56
57 void* new_ptr = realloc(ptr, new_size);
58 DC_ASSERT(new_ptr != NULL, "Standard allocator failed to realloc");
59
60 if (new_size > old_size) {
62 (char*)new_ptr + old_size, new_size - old_size);
63 }
64
65 return new_ptr;
66}
67
68DC_PUBLIC static void NS(stdalloc, deallocate)(stdalloc_ref /* ref */, void* ptr, size_t size) {
69 DC_ASSUME(ptr);
70
72
73 // JUSTIFY: Ignoring malloc clang static analyser warnings in this branch
74 // To make this safe we need to prove:
75 // 1. the ptr was allocated by self
76 //
77 // Allocators such as the hybridstatic allocator make it harder it harder
78 // - provenance: clang static analyser cannot prove a pointer's origin
79 // - can be shown by wrapping allocator's returns, but this adds substantial
80 // complexity, and makes the returned ptr-types less easy to use
81 // - can also use special headers on allocations, but this adds complexity
82 // So we settle for ignoring here:
83 //
84 // GCC's -Wfree-nonheap-object can also trigger false positives with wrapper
85 // allocators like hybridstatic that conditionally use heap vs stack storage.
86 // The runtime check (contains_ptr) prevents actual issues, but GCC's
87 // interprocedural analysis doesn't understand this.
88
89 free(ptr);
90}
91
92DC_PUBLIC static void NS(stdalloc, debug)(stdalloc const* self, dc_debug_fmt fmt, FILE* stream) {
93 DC_ASSUME(self);
94 (void)fmt;
95 fprintf(stream, "stdalloc@%p { }", (void*)self);
96}
97
98DC_PUBLIC static void NS(stdalloc, delete)(stdalloc* self) { DC_ASSUME(self); }
99
static DC_PUBLIC void deallocate(SELF *self, void *ptr, size_t size)
Definition template.h:127
static DC_PUBLIC void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:212
static DC_PUBLIC void * allocate_zeroed(SELF *self, size_t size)
Definition template.h:117
static DC_PUBLIC void * allocate_uninit(SELF *self, size_t size)
Definition template.h:92
static DC_PUBLIC void * reallocate(SELF *self, void *ptr, size_t old_size, size_t new_size)
Definition template.h:137
#define DC_TRAIT_ALLOC(SELF)
Definition trait.h:18
static DC_PUBLIC size_t size(SELF const *self)
Definition template.h:252
static DC_PUBLIC void dc_memory_tracker_set(dc_memory_tracker_level level, dc_memory_tracker_capability cap, const volatile void *addr, size_t size)
@ DC_MEMORY_TRACKER_LVL_ALLOC
@ DC_MEMORY_TRACKER_CAP_WRITE
@ DC_MEMORY_TRACKER_CAP_READ_WRITE
#define DC_PUBLIC
Definition namespace.h:25
#define NS(pre, post)
Definition namespace.h:14
#define DC_ASSERT(expr,...)
Definition panic.h:37
#define DC_ASSUME(expr,...)
Definition panic.h:57
#define DC_TRAIT_REFERENCABLE_SINGLETON(SELF, INSTANCE)
Definition ref.h:24
static stdalloc stdalloc_instance
Definition std.h:14
Debug format helpers for debug printin data structures.
Definition fmt.h:11
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108
#define DC_ZERO_SIZED(TYPE)
Zero sized types are useful as markers (e.g. for gdb printing, or to replace debug info structs.
Definition zerosized.h:11