From cc754bc6e3be0f37ca0eaca4b6b90f033433fb1a Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Mon, 29 Oct 2007 01:00:52 -0500 Subject: libalpm: introduce MALLOC and CALLOC macros These macros take the place of the common 4 or 5 line blocks of code we had in most places that called malloc or calloc. This should reduce some code duplication and make memory allocation more standard in libalpm. Highlights: * Note that the MALLOC macro actually uses calloc, this is just for safety so that memory is initialized to 0. This can be easily changed in one place. * One malloc call was completely eliminated- it made more sense to do it on the stack. * The use of RET_ERR in public functions (mainly the alpm_*_new functions) was standardized, this makes sense so pm_errno is set. Signed-off-by: Dan McGee --- lib/libalpm/server.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'lib/libalpm/server.c') diff --git a/lib/libalpm/server.c b/lib/libalpm/server.c index 11a92033..d4c0a05a 100644 --- a/lib/libalpm/server.c +++ b/lib/libalpm/server.c @@ -49,13 +49,8 @@ pmserver_t *_alpm_server_new(const char *url) ALPM_LOG_FUNC; - server = malloc(sizeof(pmserver_t)); - if(server == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmserver_t)); - RET_ERR(PM_ERR_MEMORY, NULL); - } + CALLOC(server, 1, sizeof(pmserver_t), RET_ERR(PM_ERR_MEMORY, NULL)); - memset(server, 0, sizeof(pmserver_t)); u = downloadParseURL(url); if(!u) { _alpm_log(PM_LOG_ERROR, _("url '%s' is invalid, ignoring\n"), url); @@ -122,10 +117,7 @@ static struct url *url_for_file(pmserver_t *server, const char *filename) int doclen = 0; doclen = strlen(server->s_url->doc) + strlen(filename) + 2; - doc = calloc(doclen, sizeof(char)); - if(!doc) { - RET_ERR(PM_ERR_MEMORY, NULL); - } + CALLOC(doc, doclen, sizeof(char), RET_ERR(PM_ERR_MEMORY, NULL)); snprintf(doc, doclen, "%s/%s", server->s_url->doc, filename); ret = downloadMakeURL(server->s_url->scheme, -- cgit v1.2.3