Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5338b6e

Browse files
peterepull[bot]
authored andcommitted
Expand palloc/pg_malloc API for more type safety
This adds additional variants of palloc, pg_malloc, etc. thatencapsulate common usage patterns and provide more type safety.Specifically, this adds palloc_object(), palloc_array(), andrepalloc_array(), which take the type name of the object to beallocated as its first argument and cast the return as a pointer tothat type. There are also palloc0_object() and palloc0_array()variants for initializing with zero, and pg_malloc_*() variants of allof 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 parent5186538 commit5338b6e

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
externvoid*pg_realloc(void*pointer,size_tsize);
3030
externvoidpg_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+
#definepg_malloc_object(type) ((type *) pg_malloc(sizeof(type)))
40+
#definepg_malloc0_object(type) ((type *) pg_malloc0(sizeof(type)))
41+
42+
/*
43+
* Allocate space for "count" objects of type "type"
44+
*/
45+
#definepg_malloc_array(type,count) ((type *) pg_malloc(sizeof(type) * (count)))
46+
#definepg_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+
#definepg_realloc_array(pointer,type,count) ((type *) pg_realloc(pointer, sizeof(type) * (count)))
53+
3254
/* Equivalent functions, deliberately named the same as backend functions */
3355
externchar*pstrdup(constchar*in);
3456
externchar*pnstrdup(constchar*in,Sizesize);
@@ -38,6 +60,12 @@ extern void *palloc_extended(Size size, int flags);
3860
externvoid*repalloc(void*pointer,Sizesize);
3961
externvoidpfree(void*pointer);
4062

63+
#definepalloc_object(type) ((type *) palloc(sizeof(type)))
64+
#definepalloc0_object(type) ((type *) palloc0(sizeof(type)))
65+
#definepalloc_array(type,count) ((type *) palloc(sizeof(type) * (count)))
66+
#definepalloc0_array(type,count) ((type *) palloc0(sizeof(type) * (count)))
67+
#definerepalloc_array(pointer,type,count) ((type *) repalloc(pointer, sizeof(type) * (count)))
68+
4169
/* sprintf into a palloc'd buffer --- these are in psprintf.c */
4270
externchar*psprintf(constchar*fmt,...)pg_attribute_printf(1,2);
4371
externsize_tpvsnprintf(char*buf,size_tlen,constchar*fmt,va_listargs)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
externpg_nodiscardvoid*repalloc(void*pointer,Sizesize);
8181
externvoidpfree(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+
#definepalloc_object(type) ((type *) palloc(sizeof(type)))
91+
#definepalloc0_object(type) ((type *) palloc0(sizeof(type)))
92+
93+
/*
94+
* Allocate space for "count" objects of type "type"
95+
*/
96+
#definepalloc_array(type,count) ((type *) palloc(sizeof(type) * (count)))
97+
#definepalloc0_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+
#definerepalloc_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)

[8]ページ先頭

©2009-2025 Movatter.jp