Top | ![]() |
![]() |
![]() |
![]() |
gpointer | g_slice_alloc () |
gpointer | g_slice_alloc0 () |
gpointer | g_slice_copy () |
void | g_slice_free1 () |
void | g_slice_free_chain_with_offset () |
#define | g_slice_new() |
#define | g_slice_new0() |
#define | g_slice_dup() |
#define | g_slice_free() |
#define | g_slice_free_chain() |
GSlice was a space-efficient and multi-processing scalable way to allocate
equal sized pieces of memory. Since GLib 2.76, its implementation has been
removed and it calls g_malloc()
and g_free_sized()
, because the performance
of the system-default allocators has improved on all platforms since GSlice
was written.
The GSlice APIs have not been deprecated, as they are widely in use and doing so would be very disruptive for little benefit.
New code should be written using g_new()
/g_malloc()
and g_free_sized()
or
g_free()
. There is no particular benefit in porting existing code away from
g_slice_new()
/g_slice_free()
unless it’s being rewritten anyway.
Here is an example for using the slice allocator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
gchar *mem[10000]; gint i; // Allocate 10000 blocks. for (i = 0; i < 10000; i++) { mem[i] = g_slice_alloc (50); // Fill in the memory with some junk. for (j = 0; j < 50; j++) mem[i][j] = i * j; } // Now free all of the blocks. for (i = 0; i < 10000; i++) g_slice_free1 (50, mem[i]); |
And here is an example for using the using the slice allocator with data structures:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
GRealArray *array; // Allocate one block, using the g_slice_new() macro. array = g_slice_new (GRealArray); // We can now use array just like a normal pointer to a structure. array->data = NULL; array->len = 0; array->alloc = 0; array->zero_terminated = (zero_terminated ? 1 : 0); array->clear = (clear ? 1 : 0); array->elt_size = elt_size; // We can free the block, so it can be reused. g_slice_free (GRealArray, array); |
gpointer
g_slice_alloc (gsize block_size
);
Allocates a block of memory from the libc allocator.
The block address handed out can be expected to be aligned
to at least 1 * sizeof (void*)
.
Since GLib 2.76 this always uses the system malloc()
implementation
internally.
a pointer to the allocated memory block, which will
be NULL
if and only if mem_size
is 0.
[nullable]
Since: 2.10
gpointer
g_slice_alloc0 (gsize block_size
);
Allocates a block of memory via g_slice_alloc()
and initializes
the returned memory to 0.
Since GLib 2.76 this always uses the system malloc()
implementation
internally.
a pointer to the allocated block, which will be NULL
if and only if mem_size
is 0.
[nullable]
Since: 2.10
gpointer g_slice_copy (gsize block_size
,gconstpointer mem_block
);
Allocates a block of memory from the slice allocator
and copies block_size
bytes into it from mem_block
.
mem_block
must be non-NULL
if block_size
is non-zero.
Since GLib 2.76 this always uses the system malloc()
implementation
internally.
a pointer to the allocated memory block,
which will be NULL
if and only if mem_size
is 0.
[nullable]
Since: 2.14
void g_slice_free1 (gsize block_size
,gpointer mem_block
);
Frees a block of memory.
The memory must have been allocated via g_slice_alloc()
or
g_slice_alloc0()
and the block_size
has to match the size
specified upon allocation. Note that the exact release behaviour
can be changed with the G_DEBUG=gc-friendly
environment
variable.
If mem_block
is NULL
, this function does nothing.
Since GLib 2.76 this always uses the system free_sized()
implementation
internally.
Since: 2.10
void g_slice_free_chain_with_offset (gsize block_size
,gpointer mem_chain
,gsize next_offset
);
Frees a linked list of memory blocks of structure type type
.
The memory blocks must be equal-sized, allocated via
g_slice_alloc()
or g_slice_alloc0()
and linked together by a
next
pointer (similar to GSList). The offset of the next
field in each block is passed as third argument.
Note that the exact release behaviour can be changed with the
G_DEBUG=gc-friendly
environment variable.
If mem_chain
is NULL
, this function does nothing.
Since GLib 2.76 this always uses the system free_sized()
implementation
internally.
block_size |
the size of the blocks |
|
mem_chain |
a pointer to the first block of the chain. |
[nullable] |
next_offset |
the offset of the |
Since: 2.10
#define g_slice_new(type)
A convenience macro to allocate a block of memory from the slice allocator.
It calls g_slice_alloc()
with sizeof (@type)
and casts the
returned pointer to a pointer of the given type, avoiding a type
cast in the source code.
This can never return NULL
as the minimum allocation size from
sizeof (@type)
is 1 byte.
Since GLib 2.76 this always uses the system malloc()
implementation
internally.
Since: 2.10
#define g_slice_new0(type)
A convenience macro to allocate a block of memory from the slice allocator and set the memory to 0.
It calls g_slice_alloc0()
with sizeof (@type)
and casts the returned pointer to a pointer of the given type,
avoiding a type cast in the source code.
This can never return NULL
as the minimum allocation size from
sizeof (@type)
is 1 byte.
Since GLib 2.76 this always uses the system malloc()
implementation
internally.
Since: 2.10
#define g_slice_dup(type, mem)
A convenience macro to duplicate a block of memory using the slice allocator.
It calls g_slice_copy()
with sizeof (@type)
and casts the returned pointer to a pointer of the given type,
avoiding a type cast in the source code.
This can never return NULL
.
Since GLib 2.76 this always uses the system malloc()
implementation
internally.
type |
the type to duplicate, typically a structure name |
|
mem |
the memory to copy into the allocated block. |
[not nullable] |
Since: 2.14
#define g_slice_free(type, mem)
A convenience macro to free a block of memory that has been allocated from the slice allocator.
It calls g_slice_free1()
using sizeof (type)
as the block size.
Note that the exact release behaviour can be changed with the
G_DEBUG=gc-friendly
environment variable.
If mem
is NULL
, this macro does nothing.
Since GLib 2.76 this always uses the system free()
implementation internally.
type |
the type of the block to free, typically a structure name |
|
mem |
a pointer to the block to free. |
[nullable] |
Since: 2.10
#define g_slice_free_chain(type, mem_chain, next)
Frees a linked list of memory blocks of structure type type
.
The memory blocks must be equal-sized, allocated via
g_slice_alloc()
or g_slice_alloc0()
and linked together by
a next
pointer (similar to GSList). The name of the
next
field in type
is passed as third argument.
Note that the exact release behaviour can be changed with the
G_DEBUG=gc-friendly
environment variable.
If mem_chain
is NULL
, this function does nothing.
Since GLib 2.76 this always uses the system free()
implementation internally.
type |
the type of the |
|
mem_chain |
a pointer to the first block of the chain. |
[nullable] |
next |
the field name of the next pointer in |
Since: 2.10