From 103fe49b8e410b5fee4144722f43f5bae4936fd1 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 22 Mar 2005 04:02:13 +0000 Subject: [PATCH] Add g_try_new, g_try_new0 and g_try_renew. (#169611, Stefan Kost) 2005-03-21 Matthias Clasen * glib/gmem.h: Add g_try_new, g_try_new0 and g_try_renew. (#169611, Stefan Kost) --- ChangeLog | 5 +++ ChangeLog.pre-2-10 | 5 +++ ChangeLog.pre-2-12 | 5 +++ ChangeLog.pre-2-8 | 5 +++ docs/reference/ChangeLog | 6 ++++ docs/reference/glib/glib-sections.txt | 3 ++ docs/reference/glib/tmpl/memory.sgml | 50 +++++++++++++++++++++++++-- glib/gmem.h | 7 ++++ 8 files changed, 83 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index ff113feb4..96bd57f8d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2005-03-21 Matthias Clasen + + * glib/gmem.h: Add g_try_new, g_try_new0 and + g_try_renew. (#169611, Stefan Kost) + 2005-03-20 Tor Lillqvist * glib/gspawn-win32.c (do_spawn_with_pipes): Close the process diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index ff113feb4..96bd57f8d 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,8 @@ +2005-03-21 Matthias Clasen + + * glib/gmem.h: Add g_try_new, g_try_new0 and + g_try_renew. (#169611, Stefan Kost) + 2005-03-20 Tor Lillqvist * glib/gspawn-win32.c (do_spawn_with_pipes): Close the process diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index ff113feb4..96bd57f8d 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,8 @@ +2005-03-21 Matthias Clasen + + * glib/gmem.h: Add g_try_new, g_try_new0 and + g_try_renew. (#169611, Stefan Kost) + 2005-03-20 Tor Lillqvist * glib/gspawn-win32.c (do_spawn_with_pipes): Close the process diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index ff113feb4..96bd57f8d 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,8 @@ +2005-03-21 Matthias Clasen + + * glib/gmem.h: Add g_try_new, g_try_new0 and + g_try_renew. (#169611, Stefan Kost) + 2005-03-20 Tor Lillqvist * glib/gspawn-win32.c (do_spawn_with_pipes): Close the process diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index c23fe4317..3c5f0e877 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,9 @@ +2005-03-21 Matthias Clasen + + * glib/glib-sections.txt: + * glib/tmpl/memory.sgml: Document g_try_new, g_try_new0 + and g_try_renew. + 2005-03-20 Matthias Clasen * gobject/tmpl/gparamspec.sgml: Document G_PARAM_SPEC_STATIC_ diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 0d9e58dfa..d0feee096 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -763,6 +763,9 @@ g_io_watch_funcs g_new g_new0 g_renew +g_try_new +g_try_new0 +g_try_renew g_malloc diff --git a/docs/reference/glib/tmpl/memory.sgml b/docs/reference/glib/tmpl/memory.sgml index 0c8f7c542..91836ad4f 100644 --- a/docs/reference/glib/tmpl/memory.sgml +++ b/docs/reference/glib/tmpl/memory.sgml @@ -24,7 +24,7 @@ This also means that there is no need to check if the call succeeded. Allocates @n_structs elements of type @struct_type. The returned pointer is cast to a pointer to the given type. -If @count is 0 it returns %NULL. +If @n_structs is 0 it returns %NULL. @struct_type: the type of the elements to allocate. @@ -36,7 +36,7 @@ If @count is 0 it returns %NULL. Allocates @n_structs elements of type @struct_type, initialized to 0's. The returned pointer is cast to a pointer to the given type. -If @count is 0 it returns %NULL. +If @n_structs is 0 it returns %NULL. @struct_type: the type of the elements to allocate. @@ -47,7 +47,7 @@ If @count is 0 it returns %NULL. Reallocates the memory pointed to by @mem, so that it now has space for -@n_struct elements of type @struct_type. It returns the new address of +@n_structs elements of type @struct_type. It returns the new address of the memory, which may have been moved. @@ -57,6 +57,50 @@ the memory, which may have been moved. @Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type. + + +Attempts to allocate @n_structs elements of type @struct_type, and returns +%NULL on failure. Contrast with g_new(), which aborts the program on failure. +The returned pointer is cast to a pointer to the given type. +If @n_structs is 0 it returns %NULL. + + +@struct_type: the type of the elements to allocate. +@n_structs: the number of elements to allocate. +@Returns: a pointer to the allocated memory, cast to a pointer to @struct_type. +@Since: 2.8 + + + + +Attempts to allocate @n_structs elements of type @struct_type, initialized +to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts +the program on failure. +The returned pointer is cast to a pointer to the given type. +If @n_counts is 0 it returns %NULL. + + +@struct_type: the type of the elements to allocate. +@n_structs: the number of elements to allocate. +@Returns: a pointer to the allocated memory, cast to a pointer to @struct_type. +@Since: 2.8 + + + + +Attempts to reallocate the memory pointed to by @mem, so that it now has +space for @n_structs elements of type @struct_type, and returns %NULL on +failure. Contrast with g_renew(), which aborts the program on failure. +It returns the new address of the memory, which may have been moved. + + +@struct_type: the type of the elements to allocate. +@mem: the currently allocated memory. +@n_structs: the number of elements to allocate. +@Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type. +@Since: 2.8 + + Allocates @n_bytes bytes of memory. diff --git a/glib/gmem.h b/glib/gmem.h index a1cd3e737..cf21e58f0 100644 --- a/glib/gmem.h +++ b/glib/gmem.h @@ -64,6 +64,13 @@ gpointer g_try_realloc (gpointer mem, #define g_renew(struct_type, mem, n_structs) \ ((struct_type *) g_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) +#define g_try_new(struct_type, n_structs) \ + ((struct_type *) g_try_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) +#define g_try_new0(struct_type, n_structs) \ + ((struct_type *) g_try_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) +#define g_try_renew(struct_type, mem, n_structs) \ + ((struct_type *) g_try_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) + /* Memory allocation virtualization for debugging purposes * g_mem_set_vtable() has to be the very first GLib function called