GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
include
core
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
47
struct
gp_temp_alloc
{
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) \
80
do { \
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
87
static
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 */
gp_common.h
Common macros.
gp_temp_alloc
Temporary buffer.
Definition
gp_temp_alloc.h:47
gp_temp_alloc::buffer
void * buffer
Buffer start.
Definition
gp_temp_alloc.h:49
gp_temp_alloc::pos
size_t pos
How much of buffer was consumed by allocations.
Definition
gp_temp_alloc.h:51
gp_temp_alloc::size
size_t size
Buffer size.
Definition
gp_temp_alloc.h:53
Generated by
1.9.8