Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2864f77

Browse files
committed
Expand palloc/pg_malloc API for more type safety
This adds additional variants of palloc, pg_malloc, etc. that encapsulate common usage patterns and provide more type safety. Specifically, this adds palloc_object(), palloc_array(), and repalloc_array(), which take the type name of the object to be allocated as its first argument and cast the return as a pointer to that type. There are also palloc0_object() and palloc0_array() variants for initializing with zero, and pg_malloc_*() variants of all of the above. Inspired by the talloc library. This is backpatched from master so that future backpatchable code can make use of these APIs. This patch by itself does not contain any users of these APIs. Reviewed-by: Tom Lane <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent 0081941 commit 2864f77

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/include/common/fe_memutils.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ extern void *pg_malloc_extended(size_t size, int flags);
2929
extern void *pg_realloc(void *pointer, size_t size);
3030
extern void pg_free(void *pointer);
3131

32+
/*
33+
* Variants with easier notation and more type safety
34+
*/
35+
36+
/*
37+
* Allocate space for one object of type "type"
38+
*/
39+
#define pg_malloc_object(type) ((type *) pg_malloc(sizeof(type)))
40+
#define pg_malloc0_object(type) ((type *) pg_malloc0(sizeof(type)))
41+
42+
/*
43+
* Allocate space for "count" objects of type "type"
44+
*/
45+
#define pg_malloc_array(type, count) ((type *) pg_malloc(sizeof(type) * (count)))
46+
#define pg_malloc0_array(type, count) ((type *) pg_malloc0(sizeof(type) * (count)))
47+
48+
/*
49+
* Change size of allocation pointed to by "pointer" to have space for "count"
50+
* objects of type "type"
51+
*/
52+
#define pg_realloc_array(pointer, type, count) ((type *) pg_realloc(pointer, sizeof(type) * (count)))
53+
3254
/* Equivalent functions, deliberately named the same as backend functions */
3355
extern char *pstrdup(const char *in);
3456
extern void *palloc(Size size);
@@ -37,6 +59,12 @@ extern void *palloc_extended(Size size, int flags);
3759
extern void *repalloc(void *pointer, Size size);
3860
extern void pfree(void *pointer);
3961

62+
#define palloc_object(type) ((type *) palloc(sizeof(type)))
63+
#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
64+
#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
65+
#define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
66+
#define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
67+
4068
/* sprintf into a palloc'd buffer --- these are in psprintf.c */
4169
extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
4270
extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);

src/include/utils/palloc.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ extern void *palloc_extended(Size size, int flags);
8080
extern void *repalloc(void *pointer, Size size);
8181
extern void pfree(void *pointer);
8282

83+
/*
84+
* Variants with easier notation and more type safety
85+
*/
86+
87+
/*
88+
* Allocate space for one object of type "type"
89+
*/
90+
#define palloc_object(type) ((type *) palloc(sizeof(type)))
91+
#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
92+
93+
/*
94+
* Allocate space for "count" objects of type "type"
95+
*/
96+
#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
97+
#define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
98+
99+
/*
100+
* Change size of allocation pointed to by "pointer" to have space for "count"
101+
* objects of type "type"
102+
*/
103+
#define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
104+
83105
/*
84106
* The result of palloc() is always word-aligned, so we can skip testing
85107
* alignment of the pointer when deciding which MemSet variant to use.

0 commit comments

Comments
 (0)