Derive-C
Loading...
Searching...
No Matches
std.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <derive-c/alloc/trait.h>
#include <derive-c/core/prelude.h>
#include <derive-c/core/debug/memory_tracker.h>

Go to the source code of this file.

Functions

 DC_ZERO_SIZED (stdalloc)
 An allocator for the standard allocator, which only has global state,.
 DC_TRAIT_REFERENCABLE_SINGLETON (stdalloc, stdalloc_instance)
static DC_PUBLIC void * allocate_uninit (stdalloc_ref, size_t size)
static DC_PUBLIC void * allocate_zeroed (stdalloc_ref, size_t size)
static DC_PUBLIC void * reallocate (stdalloc_ref, void *ptr, size_t old_size, size_t new_size)
static DC_PUBLIC void deallocate (stdalloc_ref, void *ptr, size_t size)
static DC_PUBLIC void debug (stdalloc const *self, dc_debug_fmt fmt, FILE *stream)
static DC_PUBLIC void delete (stdalloc *self)
 DC_TRAIT_ALLOC (stdalloc)

Variables

static stdalloc stdalloc_instance = {}

Function Documentation

◆ allocate_uninit()

DC_PUBLIC void * allocate_uninit ( stdalloc_ref ,
size_t size )
static

Definition at line 17 of file std.h.

17 {
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}
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
#define DC_ASSERT(expr,...)
Definition panic.h:37

◆ allocate_zeroed()

DC_PUBLIC void * allocate_zeroed ( stdalloc_ref ,
size_t size )
static

Definition at line 28 of file std.h.

28 {
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}
@ DC_MEMORY_TRACKER_CAP_READ_WRITE

◆ DC_TRAIT_ALLOC()

DC_TRAIT_ALLOC ( stdalloc )

◆ DC_TRAIT_REFERENCABLE_SINGLETON()

DC_TRAIT_REFERENCABLE_SINGLETON ( stdalloc ,
stdalloc_instance  )

◆ DC_ZERO_SIZED()

DC_ZERO_SIZED ( stdalloc )

An allocator for the standard allocator, which only has global state,.

◆ deallocate()

DC_PUBLIC void deallocate ( stdalloc_ref ,
void * ptr,
size_t size )
static

Definition at line 68 of file std.h.

68 {
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}
#define DC_ASSUME(expr,...)
Definition panic.h:57

◆ debug()

DC_PUBLIC void debug ( stdalloc const * self,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 92 of file std.h.

92 {
93 DC_ASSUME(self);
94 (void)fmt;
95 fprintf(stream, "stdalloc@%p { }", (void*)self);
96}
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ delete()

DC_PUBLIC void delete ( stdalloc * self)
static

Definition at line 98 of file std.h.

98{ DC_ASSUME(self); }

◆ reallocate()

DC_PUBLIC void * reallocate ( stdalloc_ref ,
void * ptr,
size_t old_size,
size_t new_size )
static

Definition at line 41 of file std.h.

42 {
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}

Variable Documentation

◆ stdalloc_instance

stdalloc stdalloc_instance = {}
static

Definition at line 14 of file std.h.

14{};