Skip to content

Commit 2016055

Browse files
committedSep 12, 2022
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. Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://www.postgresql.org/message-id/flat/bb755632-2a43-d523-36f8-a1e7a389a907@enterprisedb.com
1 parent b060f57 commit 2016055

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
 

‎src/include/common/fe_memutils.h

+28
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 char *pnstrdup(const char *in, Size size);
@@ -38,6 +60,12 @@ extern void *palloc_extended(Size size, int flags);
3860
extern void *repalloc(void *pointer, Size size);
3961
extern void pfree(void *pointer);
4062

63+
#define palloc_object(type) ((type *) palloc(sizeof(type)))
64+
#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
65+
#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
66+
#define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
67+
#define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
68+
4169
/* sprintf into a palloc'd buffer --- these are in psprintf.c */
4270
extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
4371
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

+22
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ extern void *palloc_extended(Size size, int flags);
8080
extern pg_nodiscard 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)
Please sign in to comment.