2
0

fe_memutils.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * fe_memutils.h
  3. * memory management support for frontend code
  4. *
  5. * Copyright (c) 2003-2022, PostgreSQL Global Development Group
  6. *
  7. * src/include/common/fe_memutils.h
  8. */
  9. #ifndef FE_MEMUTILS_H
  10. #define FE_MEMUTILS_H
  11. /*
  12. * Flags for pg_malloc_extended and palloc_extended, deliberately named
  13. * the same as the backend flags.
  14. */
  15. #define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB) not
  16. * actually used for frontends */
  17. #define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */
  18. #define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */
  19. /*
  20. * "Safe" memory allocation functions --- these exit(1) on failure
  21. * (except pg_malloc_extended with MCXT_ALLOC_NO_OOM)
  22. */
  23. extern char *pg_strdup(const char *in);
  24. extern void *pg_malloc(size_t size);
  25. extern void *pg_malloc0(size_t size);
  26. extern void *pg_malloc_extended(size_t size, int flags);
  27. extern void *pg_realloc(void *pointer, size_t size);
  28. extern void pg_free(void *pointer);
  29. /*
  30. * Variants with easier notation and more type safety
  31. */
  32. /*
  33. * Allocate space for one object of type "type"
  34. */
  35. #define pg_malloc_object(type) ((type *) pg_malloc(sizeof(type)))
  36. #define pg_malloc0_object(type) ((type *) pg_malloc0(sizeof(type)))
  37. /*
  38. * Allocate space for "count" objects of type "type"
  39. */
  40. #define pg_malloc_array(type, count) ((type *) pg_malloc(sizeof(type) * (count)))
  41. #define pg_malloc0_array(type, count) ((type *) pg_malloc0(sizeof(type) * (count)))
  42. /*
  43. * Change size of allocation pointed to by "pointer" to have space for "count"
  44. * objects of type "type"
  45. */
  46. #define pg_realloc_array(pointer, type, count) ((type *) pg_realloc(pointer, sizeof(type) * (count)))
  47. /* Equivalent functions, deliberately named the same as backend functions */
  48. extern char *pstrdup(const char *in);
  49. extern char *pnstrdup(const char *in, Size size);
  50. extern void *palloc(Size size);
  51. extern void *palloc0(Size size);
  52. extern void *palloc_extended(Size size, int flags);
  53. extern void *repalloc(void *pointer, Size size);
  54. extern void pfree(void *pointer);
  55. #define palloc_object(type) ((type *) palloc(sizeof(type)))
  56. #define palloc0_object(type) ((type *) palloc0(sizeof(type)))
  57. #define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
  58. #define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
  59. #define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
  60. /* sprintf into a palloc'd buffer --- these are in psprintf.c */
  61. extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
  62. extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
  63. #endif /* FE_MEMUTILS_H */