GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_temp_alloc.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2024 Cyril Hrubis <metan@ucw.cz>
4 */
5
30#ifndef CORE_GP_TEMP_ALLOC_H
31#define CORE_GP_TEMP_ALLOC_H
32
33#ifdef __linux__
34# include <alloca.h>
35#endif
36#include <stdlib.h>
37
38#include <core/gp_common.h>
39
40#ifndef GP_ALLOCA_THRESHOLD
41# define GP_ALLOCA_THRESHOLD 2048
42#endif
43
49 void *buffer;
51 size_t pos;
53 size_t size;
54};
55
56#define GP_TEMP_ALLOC(size) ({ \
57 ((size) > GP_ALLOCA_THRESHOLD) ? malloc(size) : alloca(size); \
58})
59
60#define gp_temp_alloc_create(name, bsize) \
61 struct gp_temp_alloc name = {.size = (bsize), .pos = 0, \
62 .buffer = GP_TEMP_ALLOC(bsize)};
63
64#define gp_temp_alloc_get(self, bsize) ({ \
65 GP_ASSERT(self.pos + bsize <= self.size); \
66 size_t gp_pos__ = self.pos; \
67 self.pos += bsize; \
68 (void*)(((char*)(self.buffer)) + gp_pos__); \
69})
70
71#define gp_temp_alloc_arr(self, type, len) \
72 gp_temp_alloc_get(self, sizeof(type) * len)
73
79#define gp_temp_alloc_free(self) \
80do { \
81 if (self.size > GP_ALLOCA_THRESHOLD) \
82 free(self.buffer); \
83} while (0)
84
85#define gp_temp_alloc(size) GP_TEMP_ALLOC(size)
86
87static inline void gp_temp_free(size_t size, void *ptr)
88{
89 if (size > GP_ALLOCA_THRESHOLD)
90 free(ptr);
91}
92
93#endif /* CORE_GP_TEMP_ALLOC_H */
Common macros.
Temporary buffer.
void * buffer
Buffer start.
size_t pos
How much of buffer was consumed by allocations.
size_t size
Buffer size.