gmem.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // included by glib2.pas
  2. {$IFDEF read_forward_definitions}
  3. {$ENDIF read_forward_definitions}
  4. //------------------------------------------------------------------------------
  5. {$IFDEF read_interface_rest}
  6. const
  7. G_MEM_ALIGN = GLIB_SIZEOF_VOID_P;
  8. type
  9. PGMemVTable = ^TGMemVTable;
  10. TGMemVTable = record
  11. malloc : function (n_bytes:gsize):gpointer; cdecl;
  12. realloc : function (mem:gpointer; n_bytes:gsize):gpointer; cdecl;
  13. free : procedure (mem:gpointer); cdecl;
  14. calloc : function (n_blocks:gsize; n_block_bytes:gsize):gpointer; cdecl;
  15. try_malloc : function (n_bytes:gsize):gpointer; cdecl;
  16. try_realloc : function (mem:gpointer; n_bytes:gsize):gpointer; cdecl;
  17. end;
  18. PGMemChunk = pointer; // internal structure of gmem.c
  19. PGAllocator = pointer; // internal structure of gmem.c
  20. { Memory allocation functions }
  21. function g_malloc(n_bytes:gulong):gpointer; cdecl; external gliblib;
  22. function g_malloc0(n_bytes:gulong):gpointer; cdecl; external gliblib;
  23. function g_realloc(mem:gpointer; n_bytes:gulong):gpointer; cdecl; external gliblib;
  24. procedure g_free(mem:gpointer); cdecl; external gliblib;
  25. function g_try_malloc(n_bytes:gulong):gpointer; cdecl; external gliblib;
  26. function g_try_realloc(mem:gpointer; n_bytes:gulong):gpointer; cdecl; external gliblib;
  27. { Convenience memory allocators }
  28. function g_new(bytes_per_struct, n_structs: gsize): gpointer;
  29. function g_new0(bytes_per_struct, n_structs: gsize): gpointer;
  30. function g_renew(struct_size: gsize; OldMem: gpointer; n_structs : gsize) : gpointer;
  31. { Memory allocation virtualization for debugging purposes
  32. g_mem_set_vtable() has to be the very first GLib function called
  33. if being used
  34. }
  35. { optional }
  36. procedure g_mem_set_vtable(vtable:PGMemVTable); cdecl; external gliblib;
  37. function g_mem_is_system_malloc:gboolean; cdecl; external gliblib;
  38. {$IFNDEF KYLIX}
  39. { Memory profiler and checker, has to be enabled via g_mem_set_vtable() }
  40. var
  41. glib_mem_profiler_table : PGMemVTable;cvar;external;
  42. {$ENDIF}
  43. procedure g_mem_profile; cdecl; external gliblib;
  44. { Memchunk convenience functions }
  45. { c specific:
  46. #define g_mem_chunk_create(type, pre_alloc, alloc_type) (
  47. g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")",
  48. sizeof (type), sizeof (type) * (pre_alloc), (alloc_type)) ) }
  49. function g_chunk_new(chunk : Pointer) : Pointer;
  50. function g_chunk_new0(chunk : Pointer) : Pointer;
  51. procedure g_chunk_free(mem_chunk:PGMemChunk; mem:gpointer);
  52. { "g_mem_chunk_new" creates a new memory chunk.
  53. Memory chunks are used to allocate pieces of memory which are
  54. always the same size. Lists are a good example of such a data type.
  55. The memory chunk allocates and frees blocks of memory as needed.
  56. Just be sure to call "g_mem_chunk_free" and not "g_free" on data
  57. allocated in a mem chunk. ("g_free" will most likely cause a seg
  58. fault...somewhere).
  59. Oh yeah, GMemChunk is an opaque data type. (You don't really
  60. want to know what's going on inside do you?)
  61. }
  62. { ALLOC_ONLY MemChunk's can only allocate memory. The free operation
  63. is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
  64. atom. (They are also useful for lists which use MemChunk to allocate
  65. memory but are also part of the MemChunk implementation).
  66. ALLOC_AND_FREE MemChunk's can allocate and free memory.
  67. }
  68. const
  69. G_ALLOC_ONLY = 1;
  70. G_ALLOC_AND_FREE = 2;
  71. function g_mem_chunk_new(name:Pgchar; atom_size:gint; area_size:gulong; _type:gint):PGMemChunk; cdecl; external gliblib;
  72. procedure g_mem_chunk_destroy(mem_chunk:PGMemChunk); cdecl; external gliblib;
  73. function g_mem_chunk_alloc(mem_chunk:PGMemChunk):gpointer; cdecl; external gliblib;
  74. function g_mem_chunk_alloc0(mem_chunk:PGMemChunk):gpointer; cdecl; external gliblib;
  75. procedure g_mem_chunk_free(mem_chunk:PGMemChunk; mem:gpointer); cdecl; external gliblib;
  76. procedure g_mem_chunk_clean(mem_chunk:PGMemChunk); cdecl; external gliblib;
  77. procedure g_mem_chunk_reset(mem_chunk:PGMemChunk); cdecl; external gliblib;
  78. procedure g_mem_chunk_print(mem_chunk:PGMemChunk); cdecl; external gliblib;
  79. procedure g_mem_chunk_info; cdecl; external gliblib;
  80. { Ah yes...we have a "g_blow_chunks" function.
  81. "g_blow_chunks" simply compresses all the chunks. This operation
  82. consists of freeing every memory area that should be freed (but
  83. which we haven't gotten around to doing yet). And, no,
  84. "g_blow_chunks" doesn't follow the naming scheme, but it is a
  85. much better name than "g_mem_chunk_clean_all" or something
  86. similar.
  87. }
  88. procedure g_blow_chunks; cdecl; external gliblib;
  89. { Generic allocators }
  90. function g_allocator_new(name:Pgchar; n_preallocs:guint):PGAllocator; cdecl; external gliblib;
  91. procedure g_allocator_free(allocator:PGAllocator); cdecl; external gliblib;
  92. { internal }
  93. const
  94. G_ALLOCATOR_LIST = 1;
  95. G_ALLOCATOR_SLIST = 2;
  96. G_ALLOCATOR_NODE = 3;
  97. {$ENDIF read_interface_rest}
  98. // included by glib2.pas