Class PtrArray
-
Constructor Summary
ConstructorDescriptionPtrArray()
Allocate a new PtrArray.Allocate a new PtrArray.PtrArray
(MemorySegment address) Create a PtrArray proxy instance for the provided memory address.PtrArray
(MemorySegment pdata, int len) Allocate a new PtrArray with the fields set to the provided values.PtrArray
(MemorySegment pdata, int len, Arena arena) Allocate a new PtrArray with the fields set to the provided values. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
add
(MemorySegment[] array, @Nullable MemorySegment data) Adds a pointer to the end of the pointer array.static MemorySegment[]
copy
(MemorySegment[] array, @Nullable CopyFunc func) Makes a full (deep) copy of aGPtrArray
.static void
extend
(MemorySegment[] arrayToExtend, MemorySegment[] array, @Nullable CopyFunc func) Adds all pointers ofarray
to the end of the arrayarrayToExtend
.static void
extendAndSteal
(MemorySegment[] arrayToExtend, MemorySegment[] array) Adds all the pointers inarray
to the end ofarrayToExtend
, transferring ownership of each element fromarray
toarrayToExtend
and modifyingarrayToExtend
in-place.static boolean
find
(MemorySegment[] haystack, @Nullable MemorySegment needle, @Nullable Out<Integer> index) Checks whetherneedle
exists inhaystack
.static boolean
findWithEqualFunc
(MemorySegment[] haystack, @Nullable MemorySegment needle, @Nullable EqualFunc equalFunc, @Nullable Out<Integer> index) Checks whetherneedle
exists inhaystack
, using the givenequalFunc
.static void
foreach
(MemorySegment[] array, Func func) Calls a function for each element of aGPtrArray
.static MemorySegment
free
(MemorySegment[] array, boolean freeSeg) Frees the memory allocated for theGPtrArray
.static MemoryLayout
The memory layout of the native struct.static Type
getType()
Get the GType of the PtrArray classstatic void
insert
(MemorySegment[] array, int index, @Nullable MemorySegment data) Inserts an element into the pointer array at the given index.static boolean
isNullTerminated
(MemorySegment[] array) Gets whether thearray
was constructed asnull
-terminated.static MemorySegment[]
new_()
Creates a newGPtrArray
with a reference count of 1.static MemorySegment[]
newFromArray
(@Nullable MemorySegment[] data, @Nullable CopyFunc copyFunc) Creates a newGPtrArray
, copyinglen
pointers fromdata
, and setting the array’s reference count to 1.static MemorySegment[]
newFromNullTerminatedArray
(@Nullable MemorySegment[] data, @Nullable CopyFunc copyFunc) Creates a newGPtrArray
copying the pointers fromdata
after having computed the length of it and with a reference count of 1.static MemorySegment[]
newFull
(int reservedSize) Creates a newGPtrArray
withreservedSize
pointers preallocated and a reference count of 1.static MemorySegment[]
newNullTerminated
(int reservedSize, boolean nullTerminated) Like g_ptr_array_new_full() but also allows to set the array to benull
terminated.static MemorySegment[]
newTake
(@Nullable MemorySegment[] data) Creates a newGPtrArray
withdata
as pointers,len
as length and a reference count of 1.static MemorySegment[]
newTakeNullTerminated
(@Nullable MemorySegment[] data) Creates a newGPtrArray
withdata
as pointers, computing the length of it and setting the reference count to 1.static MemorySegment[]
Creates a newGPtrArray
with a reference count of 1 and useelementFreeFunc
for freeing each element when the array is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called withfreeSegment
set totrue
or when removing elements.int
readLen()
Read the value of the fieldlen
.Read the value of the fieldpdata
.static MemorySegment[]
ref
(MemorySegment[] array) Atomically increments the reference count ofarray
by one.static boolean
remove
(MemorySegment[] array, @Nullable MemorySegment data) Removes the first occurrence of the given pointer from the pointer array.static boolean
removeFast
(MemorySegment[] array, @Nullable MemorySegment data) Removes the first occurrence of the given pointer from the pointer array.static MemorySegment
removeIndex
(MemorySegment[] array, int index) Removes the pointer at the given index from the pointer array.static MemorySegment
removeIndexFast
(MemorySegment[] array, int index) Removes the pointer at the given index from the pointer array.static MemorySegment[]
removeRange
(MemorySegment[] array, int index, int length) Removes the given number of pointers starting at the given index from aGPtrArray
.static void
setFreeFunc
(MemorySegment[] array) Sets a function for freeing each element whenarray
is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called withfreeSegment
set totrue
or when removing elements.static void
setSize
(MemorySegment[] array, int length) Sets the size of the array.static MemorySegment[]
sizedNew
(int reservedSize) Creates a newGPtrArray
withreservedSize
pointers preallocated and a reference count of 1.static void
sort
(MemorySegment[] array, CompareFunc compareFunc) Sorts the array, usingcompareFunc
which should be a qsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater than second arg).static void
sortValues
(MemorySegment[] array, CompareFunc compareFunc) Sorts the array, usingcompareFunc
which should be a qsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater than second arg).static void
sortValuesWithData
(MemorySegment[] array, CompareDataFunc compareFunc) Like g_ptr_array_sort_values(), but the comparison function has an extra user data argument.static void
sortWithData
(MemorySegment[] array, CompareDataFunc compareFunc) Like g_ptr_array_sort(), but the comparison function has an extra user data argument.static MemorySegment
steal
(MemorySegment[] array, @Nullable Out<Long> len) Frees the data in the array and resets the size to zero, while the underlying array is preserved for use elsewhere and returned to the caller.static MemorySegment
stealIndex
(MemorySegment[] array, int index) Removes the pointer at the given index from the pointer array.static MemorySegment
stealIndexFast
(MemorySegment[] array, int index) Removes the pointer at the given index from the pointer array.static void
unref
(MemorySegment[] array) Atomically decrements the reference count ofarray
by one.void
writeLen
(int len) Write a value in the fieldlen
.void
writePdata
(MemorySegment pdata) Write a value in the fieldpdata
.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
PtrArray
Create a PtrArray proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
PtrArray
Allocate a new PtrArray.- Parameters:
arena
- to control the memory allocation scope
-
PtrArray
public PtrArray()Allocate a new PtrArray. The memory is allocated withArena.ofAuto()
. -
PtrArray
Allocate a new PtrArray with the fields set to the provided values.- Parameters:
pdata
- value for the fieldpdata
len
- value for the fieldlen
arena
- to control the memory allocation scope
-
PtrArray
Allocate a new PtrArray with the fields set to the provided values. The memory is allocated withArena.ofAuto()
.- Parameters:
pdata
- value for the fieldpdata
len
- value for the fieldlen
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readPdata
Read the value of the fieldpdata
.- Returns:
- The value of the field
pdata
-
writePdata
Write a value in the fieldpdata
.- Parameters:
pdata
- The new value for the fieldpdata
-
readLen
public int readLen()Read the value of the fieldlen
.- Returns:
- The value of the field
len
-
writeLen
public void writeLen(int len) Write a value in the fieldlen
.- Parameters:
len
- The new value for the fieldlen
-
add
Adds a pointer to the end of the pointer array. The array will grow in size automatically if necessary.- Parameters:
array
- aGPtrArray
data
- the pointer to add
-
copy
Makes a full (deep) copy of aGPtrArray
.func
, as aGCopyFunc
, takes two arguments, the data to be copied and auserData
pointer. On common processor architectures, it's safe to passnull
asuserData
if the copy function takes only one argument. You may get compiler warnings from this though if compiling with GCC’s-Wcast-function-type
warning.If
func
isnull
, then only the pointers (and not what they are pointing to) are copied to the newGPtrArray
.The copy of
array
will have the sameGDestroyNotify
for its elements asarray
. The copy will also benull
terminated if (and only if) the source array is.- Parameters:
array
-GPtrArray
to duplicatefunc
- a copy function used to copy every element in the array- Returns:
- a deep copy of the initial
GPtrArray
.
-
extend
public static void extend(MemorySegment[] arrayToExtend, MemorySegment[] array, @Nullable @Nullable CopyFunc func) Adds all pointers ofarray
to the end of the arrayarrayToExtend
. The array will grow in size automatically if needed.arrayToExtend
is modified in-place.func
, as aGCopyFunc
, takes two arguments, the data to be copied and auserData
pointer. On common processor architectures, it's safe to passnull
asuserData
if the copy function takes only one argument. You may get compiler warnings from this though if compiling with GCC’s-Wcast-function-type
warning.If
func
isnull
, then only the pointers (and not what they are pointing to) are copied to the newGPtrArray
.Whether
arrayToExtend
isnull
terminated stays unchanged by this function.- Parameters:
arrayToExtend
- aGPtrArray
.array
- aGPtrArray
to add to the end ofarrayToExtend
.func
- a copy function used to copy every element in the array
-
extendAndSteal
Adds all the pointers inarray
to the end ofarrayToExtend
, transferring ownership of each element fromarray
toarrayToExtend
and modifyingarrayToExtend
in-place.array
is then freed.As with g_ptr_array_free(),
array
will be destroyed if its reference count is 1. If its reference count is higher, it will be decremented and the length ofarray
set to zero.- Parameters:
arrayToExtend
- aGPtrArray
.array
- aGPtrArray
to add to the end ofarrayToExtend
.
-
find
public static boolean find(MemorySegment[] haystack, @Nullable @Nullable MemorySegment needle, @Nullable @Nullable Out<Integer> index) Checks whetherneedle
exists inhaystack
. If the element is found,true
is returned and the element’s index is returned inindex
(if non-null
). Otherwise,false
is returned andindex
is undefined. Ifneedle
exists multiple times inhaystack
, the index of the first instance is returned.This does pointer comparisons only. If you want to use more complex equality checks, such as string comparisons, use g_ptr_array_find_with_equal_func().
- Parameters:
haystack
- pointer array to be searchedneedle
- pointer to look forindex
- return location for the index of the element, if found- Returns:
true
ifneedle
is one of the elements ofhaystack
-
findWithEqualFunc
public static boolean findWithEqualFunc(MemorySegment[] haystack, @Nullable @Nullable MemorySegment needle, @Nullable @Nullable EqualFunc equalFunc, @Nullable @Nullable Out<Integer> index) Checks whetherneedle
exists inhaystack
, using the givenequalFunc
. If the element is found,true
is returned and the element’s index is returned inindex
(if non-null
). Otherwise,false
is returned andindex
is undefined. Ifneedle
exists multiple times inhaystack
, the index of the first instance is returned.equalFunc
is called with the element from the array as its first parameter, andneedle
as its second parameter. IfequalFunc
isnull
, pointer equality is used.- Parameters:
haystack
- pointer array to be searchedneedle
- pointer to look forequalFunc
- the function to call for each element, which should returntrue
when the desired element is found; ornull
to use pointer equalityindex
- return location for the index of the element, if found- Returns:
true
ifneedle
is one of the elements ofhaystack
-
foreach
Calls a function for each element of aGPtrArray
.func
must not add elements to or remove elements from the array.- Parameters:
array
- aGPtrArray
func
- the function to call for each array element
-
free
Frees the memory allocated for theGPtrArray
. IffreeSeg
istrue
it frees the memory block holding the elements as well. Passfalse
if you want to free theGPtrArray
wrapper but preserve the underlying array for use elsewhere. If the reference count ofarray
is greater than one, theGPtrArray
wrapper is preserved but the size ofarray
will be set to zero.If array contents point to dynamically-allocated memory, they should be freed separately if
freeSeg
istrue
and noGDestroyNotify
function has been set forarray
.Note that if the array is
null
terminated andfreeSeg
isfalse
then this will always return an allocatednull
terminated buffer. If pdata is previouslynull
, a new buffer will be allocated.This function is not thread-safe. If using a
GPtrArray
from multiple threads, use only the atomic g_ptr_array_ref() and g_ptr_array_unref() functions.- Parameters:
array
- aGPtrArray
freeSeg
- iftrue
the actual pointer array is freed as well- Returns:
- the pointer array if
freeSeg
isfalse
, otherwisenull
. The pointer array should be freed using g_free().
-
insert
Inserts an element into the pointer array at the given index. The array will grow in size automatically if necessary.- Parameters:
array
- aGPtrArray
index
- the index to place the new element at, or -1 to appenddata
- the pointer to add.
-
isNullTerminated
Gets whether thearray
was constructed asnull
-terminated.This will only return
true
for arrays constructed by passingtrue
to thenull_terminated
argument of g_ptr_array_new_null_terminated(). It will not returntrue
for normal arrays which have had anull
element appended to them.- Parameters:
array
- theGPtrArray
- Returns:
true
if the array is made to benull
terminated.
-
new_
Creates a newGPtrArray
with a reference count of 1.- Returns:
- the new
GPtrArray
-
newFromArray
public static MemorySegment[] newFromArray(@Nullable @Nullable MemorySegment[] data, @Nullable @Nullable CopyFunc copyFunc) Creates a newGPtrArray
, copyinglen
pointers fromdata
, and setting the array’s reference count to 1.This avoids having to manually add each element one by one.
If
copyFunc
is provided, then it is used to copy each element before adding them to the new array. If it isnull
then the pointers are copied directly.It also sets
elementFreeFunc
for freeing each element when the array is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called withfreeSegment
set totrue
or when removing elements.Do not use it if
len
is greater thanG_MAXUINT
.GPtrArray
stores the length of its data inguint
, which may be shorter thangsize
.- Parameters:
data
- an array of pointers, ornull
for an empty arraycopyFunc
- a copy function used to copy every element in the array ornull
.- Returns:
- A new
GPtrArray
-
newFromNullTerminatedArray
public static MemorySegment[] newFromNullTerminatedArray(@Nullable @Nullable MemorySegment[] data, @Nullable @Nullable CopyFunc copyFunc) Creates a newGPtrArray
copying the pointers fromdata
after having computed the length of it and with a reference count of 1. This avoids having to manually add each element one by one. IfcopyFunc
is provided, then it is used to copy the data in the new array. It also setelementFreeFunc
for freeing each element when the array is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called withfreeSegment
set totrue
or when removing elements.Do not use it if the
data
has more thanG_MAXUINT
elements.GPtrArray
stores the length of its data inguint
, which may be shorter thangsize
.- Parameters:
data
- an array of pointers,null
terminated; ornull
for an empty arraycopyFunc
- a copy function used to copy every element in the array ornull
.- Returns:
- A new
GPtrArray
-
newFull
Creates a newGPtrArray
withreservedSize
pointers preallocated and a reference count of 1. This avoids frequent reallocation, if you are going to add many pointers to the array. Note however that the size of the array is still 0. It also setelementFreeFunc
for freeing each element when the array is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called withfreeSegment
set totrue
or when removing elements.- Parameters:
reservedSize
- number of pointers preallocated- Returns:
- A new
GPtrArray
-
newNullTerminated
Like g_ptr_array_new_full() but also allows to set the array to benull
terminated. Anull
terminated pointer array has an additionalnull
pointer after the last element, beyond the current length.GPtrArray
created by other constructors are not automaticallynull
terminated.Note that if the
array
's length is zero and currently no data array is allocated, then pdata will still benull
.GPtrArray
will onlynull
terminate pdata, if an actual array is allocated. It does not guarantee that an array is always allocated. In other words, if the length is zero, then pdata may either point to anull
terminated array of length zero or benull
.- Parameters:
reservedSize
- number of pointers preallocated. IfnullTerminated
istrue
, the actually allocated buffer size isreservedSize
plus 1, unlessreservedSize
is zero, in which case no initial buffer gets allocated.nullTerminated
- whether to make the array asnull
terminated.- Returns:
- A new
GPtrArray
-
newTake
Creates a newGPtrArray
withdata
as pointers,len
as length and a reference count of 1.This avoids having to copy such data manually. After this call,
data
belongs to theGPtrArray
and may no longer be modified by the caller. The memory ofdata
has to be dynamically allocated and will eventually be freed with g_free().It also sets
elementFreeFunc
for freeing each element when the array is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called withfreeSegment
set totrue
or when removing elements.Do not use it if
len
is greater thanG_MAXUINT
.GPtrArray
stores the length of its data inguint
, which may be shorter thangsize
.- Parameters:
data
- an array of pointers, ornull
for an empty array- Returns:
- A new
GPtrArray
-
newTakeNullTerminated
Creates a newGPtrArray
withdata
as pointers, computing the length of it and setting the reference count to 1.This avoids having to copy such data manually. After this call,
data
belongs to theGPtrArray
and may no longer be modified by the caller. The memory ofdata
has to be dynamically allocated and will eventually be freed with g_free().The length is calculated by iterating through
data
until the firstnull
element is found.It also sets
elementFreeFunc
for freeing each element when the array is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called withfreeSegment
set totrue
or when removing elements.Do not use it if the
data
length is greater thanG_MAXUINT
.GPtrArray
stores the length of its data inguint
, which may be shorter thangsize
.- Parameters:
data
- an array of pointers,null
terminated, ornull
for an empty array- Returns:
- A new
GPtrArray
-
newWithFreeFunc
Creates a newGPtrArray
with a reference count of 1 and useelementFreeFunc
for freeing each element when the array is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called withfreeSegment
set totrue
or when removing elements.- Returns:
- A new
GPtrArray
-
ref
Atomically increments the reference count ofarray
by one. This function is thread-safe and may be called from any thread.- Parameters:
array
- aGPtrArray
- Returns:
- The passed in
GPtrArray
-
remove
Removes the first occurrence of the given pointer from the pointer array. The following elements are moved down one place. Ifarray
has a non-null
GDestroyNotify
function it is called for the removed element.It returns
true
if the pointer was removed, orfalse
if the pointer was not found.- Parameters:
array
- aGPtrArray
data
- the pointer to remove- Returns:
true
if the pointer is removed,false
if the pointer is not found in the array
-
removeFast
Removes the first occurrence of the given pointer from the pointer array. The last element in the array is used to fill in the space, so this function does not preserve the order of the array. But it is faster than g_ptr_array_remove(). Ifarray
has a non-null
GDestroyNotify
function it is called for the removed element.It returns
true
if the pointer was removed, orfalse
if the pointer was not found.- Parameters:
array
- aGPtrArray
data
- the pointer to remove- Returns:
true
if the pointer was found in the array
-
removeIndex
Removes the pointer at the given index from the pointer array. The following elements are moved down one place. Ifarray
has a non-null
GDestroyNotify
function it is called for the removed element. If so, the return value from this function will potentially point to freed memory (depending on theGDestroyNotify
implementation).- Parameters:
array
- aGPtrArray
index
- the index of the pointer to remove- Returns:
- the pointer which was removed
-
removeIndexFast
Removes the pointer at the given index from the pointer array. The last element in the array is used to fill in the space, so this function does not preserve the order of the array. But it is faster than g_ptr_array_remove_index(). Ifarray
has a non-null
GDestroyNotify
function it is called for the removed element. If so, the return value from this function will potentially point to freed memory (depending on theGDestroyNotify
implementation).- Parameters:
array
- aGPtrArray
index
- the index of the pointer to remove- Returns:
- the pointer which was removed
-
removeRange
Removes the given number of pointers starting at the given index from aGPtrArray
. The following elements are moved to close the gap. Ifarray
has a non-null
GDestroyNotify
function it is called for the removed elements.- Parameters:
array
- aGPtrArray
index
- the index of the first pointer to removelength
- the number of pointers to remove- Returns:
- the
array
-
setFreeFunc
Sets a function for freeing each element whenarray
is destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called withfreeSegment
set totrue
or when removing elements.- Parameters:
array
- AGPtrArray
-
setSize
Sets the size of the array. When making the array larger, newly-added elements will be set tonull
. When making it smaller, ifarray
has a non-null
GDestroyNotify
function then it will be called for the removed elements.- Parameters:
array
- aGPtrArray
length
- the new length of the pointer array
-
sizedNew
Creates a newGPtrArray
withreservedSize
pointers preallocated and a reference count of 1. This avoids frequent reallocation, if you are going to add many pointers to the array. Note however that the size of the array is still 0.- Parameters:
reservedSize
- number of pointers preallocated- Returns:
- the new
GPtrArray
-
sort
Sorts the array, usingcompareFunc
which should be a qsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater than second arg).Note that the comparison function for g_ptr_array_sort() doesn't take the pointers from the array as arguments, it takes pointers to the pointers in the array.
Use g_ptr_array_sort_values() if you want to use normal
GCompareFuncs
, otherwise here is a full example of use:typedef struct { gchar *name; gint size; } FileListEntry; static gint sort_filelist (gconstpointer a, gconstpointer b) { const FileListEntry *entry1 = *((FileListEntry **) a); const FileListEntry *entry2 = *((FileListEntry **) b); return g_ascii_strcasecmp (entry1->name, entry2->name); } … g_autoptr (GPtrArray) file_list = NULL; // initialize file_list array and load with many FileListEntry entries ... // now sort it with g_ptr_array_sort (file_list, sort_filelist);
This is guaranteed to be a stable sort since version 2.32.
- Parameters:
array
- aGPtrArray
compareFunc
- comparison function
-
sortValues
Sorts the array, usingcompareFunc
which should be a qsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater than second arg).This is guaranteed to be a stable sort.
- Parameters:
array
- aGPtrArray
compareFunc
- aGCompareFunc
comparison function
-
sortValuesWithData
Like g_ptr_array_sort_values(), but the comparison function has an extra user data argument.This is guaranteed to be a stable sort.
- Parameters:
array
- aGPtrArray
compareFunc
- aGCompareDataFunc
comparison function
-
sortWithData
Like g_ptr_array_sort(), but the comparison function has an extra user data argument.Note that the comparison function for g_ptr_array_sort_with_data() doesn't take the pointers from the array as arguments, it takes pointers to the pointers in the array.
Use g_ptr_array_sort_values_with_data() if you want to use normal
GCompareDataFuncs
, otherwise here is a full example of use:typedef enum { SORT_NAME, SORT_SIZE } SortMode; typedef struct { gchar *name; gint size; } FileListEntry; static gint sort_filelist (gconstpointer a, gconstpointer b, gpointer user_data) { gint order; const SortMode sort_mode = GPOINTER_TO_INT (user_data); const FileListEntry *entry1 = *((FileListEntry **) a); const FileListEntry *entry2 = *((FileListEntry **) b); switch (sort_mode) { case SORT_NAME: order = g_ascii_strcasecmp (entry1->name, entry2->name); break; case SORT_SIZE: order = entry1->size - entry2->size; break; default: order = 0; break; } return order; } ... g_autoptr (GPtrArray) file_list = NULL; SortMode sort_mode; // initialize file_list array and load with many FileListEntry entries ... // now sort it with sort_mode = SORT_NAME; g_ptr_array_sort_with_data (file_list, sort_filelist, GINT_TO_POINTER (sort_mode));
This is guaranteed to be a stable sort since version 2.32.
- Parameters:
array
- aGPtrArray
compareFunc
- comparison function
-
steal
Frees the data in the array and resets the size to zero, while the underlying array is preserved for use elsewhere and returned to the caller.Note that if the array is
null
terminated this may still returnnull
if the length of the array was zero and pdata was not yet allocated.Even if set, the
GDestroyNotify
function will never be called on the current contents of the array and the caller is responsible for freeing the array elements.An example of use:
g_autoptr(GPtrArray) chunk_buffer = g_ptr_array_new_with_free_func (g_bytes_unref); // Some part of your application appends a number of chunks to the pointer array. g_ptr_array_add (chunk_buffer, g_bytes_new_static ("hello", 5)); g_ptr_array_add (chunk_buffer, g_bytes_new_static ("world", 5)); … // Periodically, the chunks need to be sent as an array-and-length to some // other part of the program. GBytes **chunks; gsize n_chunks; chunks = g_ptr_array_steal (chunk_buffer, &n_chunks); for (gsize i = 0; i < n_chunks; i++) { // Do something with each chunk here, and then free them, since // g_ptr_array_steal() transfers ownership of all the elements and the // array to the caller. … g_bytes_unref (chunks[i]); } g_free (chunks); // After calling g_ptr_array_steal(), the pointer array can be reused for the // next set of chunks. g_assert (chunk_buffer->len == 0);
- Parameters:
array
- aGPtrArray
.len
- pointer to retrieve the number of elements of the original array- Returns:
- the element data, which should be
freed using g_free(). This may be
null
if the array doesn’t have any elements (i.e. if*len
is zero).
-
stealIndex
Removes the pointer at the given index from the pointer array. The following elements are moved down one place. TheGDestroyNotify
forarray
is not called on the removed element; ownership is transferred to the caller of this function.- Parameters:
array
- aGPtrArray
index
- the index of the pointer to steal- Returns:
- the pointer which was removed
-
stealIndexFast
Removes the pointer at the given index from the pointer array. The last element in the array is used to fill in the space, so this function does not preserve the order of the array. But it is faster than g_ptr_array_steal_index(). TheGDestroyNotify
forarray
is not called on the removed element; ownership is transferred to the caller of this function.- Parameters:
array
- aGPtrArray
index
- the index of the pointer to steal- Returns:
- the pointer which was removed
-
unref
Atomically decrements the reference count ofarray
by one. If the reference count drops to 0, the effect is the same as calling g_ptr_array_free() withfreeSegment
set totrue
. This function is thread-safe and may be called from any thread.- Parameters:
array
- AGPtrArray
-