diff options
| author | Johan Hedberg <johan.hedberg@nokia.com> | 2007-01-20 11:50:05 +0000 | 
|---|---|---|
| committer | Johan Hedberg <johan.hedberg@nokia.com> | 2007-01-20 11:50:05 +0000 | 
| commit | 3fd70cad862febf8f1a60bd47576cb758d085958 (patch) | |
| tree | 9c05c461b9799916690affcef548b9ca34b13e7c | |
| parent | acf8efe67b57a94494a5e2624d131e071445d917 (diff) | |
Implement memory allocation functions for eglib
| -rw-r--r-- | eglib/gmain.c | 75 | ||||
| -rw-r--r-- | eglib/gmain.h | 20 | 
2 files changed, 95 insertions, 0 deletions
| diff --git a/eglib/gmain.c b/eglib/gmain.c index a3bf3760..bc1af91a 100644 --- a/eglib/gmain.c +++ b/eglib/gmain.c @@ -813,3 +813,78 @@ void g_slist_free(GSList *list)  		free(l);  	}  } + +/* Memory allocation functions */ + +gpointer g_malloc(gulong n_bytes) +{ +	gpointer mem; + +	if (!n_bytes) +		return NULL; + +	mem = malloc((size_t) n_bytes); +	if (!mem) { +		fprintf(stderr, "g_malloc: failed to allocate %lu bytes", +				n_bytes); +		abort(); +	} + +	return mem; +} + +gpointer g_malloc0(gulong n_bytes) +{ +	gpointer mem; + +	if (!n_bytes) +		return NULL; + +	mem = g_malloc(n_bytes); + +	memset(mem, 0, (size_t) n_bytes); + +	return mem; +} + +gpointer g_try_malloc(gulong n_bytes) +{ +	if (!n_bytes) +		return NULL; + +	return malloc((size_t) n_bytes); +} + +gpointer g_try_malloc0(gulong n_bytes) +{ +	gpointer mem; + +	mem = g_try_malloc(n_bytes); +	if (mem) +		memset(mem, 0, (size_t) n_bytes); + +	return mem; +} + +void g_free(gpointer mem) +{ +	if (mem) +		free(mem); +} + +gchar *g_strdup(const gchar *str) +{ +	gchar *s; + +	if (!str) +		return NULL; + +	s = strdup(str); +	if (!s) { +		fprintf(stderr, "strdup: failed to allocate new string"); +		abort(); +	} + +	return s; +} + diff --git a/eglib/gmain.h b/eglib/gmain.h index 950d4170..c86ab1eb 100644 --- a/eglib/gmain.h +++ b/eglib/gmain.h @@ -136,4 +136,24 @@ void g_slist_free(GSList *list);  /* End GSList declarations */ +/* Memory allocation related */ + +gpointer g_malloc(gulong n_bytes); +gpointer g_malloc0(gulong n_bytes); +gpointer g_try_malloc(gulong n_bytes); +gpointer g_try_malloc0(gulong n_bytes); + +void g_free(gpointer mem); + +gchar *g_strdup(const gchar *str); + +#define g_new(struct_type, n_structs) \ +	((struct_type *) g_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) +#define g_new0(struct_type, n_structs) \ +	((struct_type *) g_malloc0 (((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)))) +  #endif /* __GMAIN_H */ | 
