Constructor

GioMemoryOutputStreamnew

Declaration [src]

GOutputStream*
g_memory_output_stream_new (
  gpointer data,
  gsize size,
  GReallocFunc realloc_function,
  GDestroyNotify destroy_function
)

Description [src]

Creates a new GMemoryOutputStream.

In most cases this is not the function you want. See g_memory_output_stream_new_resizable() instead.

If data is non-NULL, the stream will use that for its internal storage.

If realloc_fn is non-NULL, it will be used for resizing the internal storage when necessary and the stream will be considered resizable. In that case, the stream will start out being (conceptually) empty. size is used only as a hint for how big data is. Specifically, seeking to the end of a newly-created stream will seek to zero, not size. Seeking past the end of the stream and then writing will introduce a zero-filled gap.

If realloc_fn is NULL then the stream is fixed-sized. Seeking to the end will seek to size exactly. Writing past the end will give an ‘out of space’ error. Attempting to seek past the end will fail. Unlike the resizable case, seeking to an offset within the stream and writing will preserve the bytes passed in as data before that point and will return them as part of g_memory_output_stream_steal_data(). If you intend to seek you should probably therefore ensure that data is properly initialised.

It is probably only meaningful to provide data and size in the case that you want a fixed-sized stream. Put another way: if realloc_fn is non-NULL then it makes most sense to give data as NULL and size as 0 (allowing GMemoryOutputStream to do the initial allocation for itself).

// a stream that can grow
stream = g_memory_output_stream_new (NULL, 0, realloc, free);

// another stream that can grow
stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);

// a fixed-size stream
data = malloc (200);
stream3 = g_memory_output_stream_new (data, 200, NULL, free);

This constructor is not directly available to language bindings.

Parameters

data

Type: gpointer

Pointer to a chunk of memory to use, or NULL.

The argument can be NULL.
The data is owned by the caller of the function.
size

Type: gsize

The size of data.

realloc_function

Type: GReallocFunc

A function with realloc() semantics (like g_realloc()) to be called when data needs to be grown, or NULL.

The argument can be NULL.
destroy_function

Type: GDestroyNotify

A function to be called on data when the stream is finalized, or NULL.

The argument can be NULL.

Return value

Type: GOutputStream

A newly created GMemoryOutputStream object.

The caller of the function takes ownership of the data, and is responsible for freeing it.