aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--astmm.c63
-rw-r--r--autoservice.c31
-rw-r--r--callerid.c58
-rw-r--r--cdr.c24
-rw-r--r--channel.c44
-rw-r--r--chanvars.c18
6 files changed, 97 insertions, 141 deletions
diff --git a/astmm.c b/astmm.c
index 385128d78..122504a58 100644
--- a/astmm.c
+++ b/astmm.c
@@ -41,13 +41,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#define SOME_PRIME 563
-#define FUNC_CALLOC 1
-#define FUNC_MALLOC 2
-#define FUNC_REALLOC 3
-#define FUNC_STRDUP 4
-#define FUNC_STRNDUP 5
-#define FUNC_VASPRINTF 6
-#define FUNC_ASPRINTF 7
+enum func_type {
+ FUNC_CALLOC = 1,
+ FUNC_MALLOC,
+ FUNC_REALLOC,
+ FUNC_STRDUP,
+ FUNC_STRNDUP,
+ FUNC_VASPRINTF,
+ FUNC_ASPRINTF
+};
/* Undefine all our macros */
#undef malloc
@@ -68,7 +70,7 @@ static struct ast_region {
char file[40];
char func[40];
int lineno;
- int which;
+ enum func_type which;
size_t len;
unsigned int fence;
unsigned char data[0];
@@ -80,13 +82,13 @@ static struct ast_region {
AST_MUTEX_DEFINE_STATIC(reglock);
AST_MUTEX_DEFINE_STATIC(showmemorylock);
-static inline void *__ast_alloc_region(size_t size, int which, const char *file, int lineno, const char *func)
+static inline void *__ast_alloc_region(size_t size, const enum func_type which, const char *file, int lineno, const char *func)
{
struct ast_region *reg;
void *ptr = NULL;
unsigned int *fence;
int hash;
- reg = malloc(size + sizeof(struct ast_region) + sizeof(unsigned int));
+ reg = malloc(size + sizeof(*reg) + sizeof(*fence));
ast_mutex_lock(&reglock);
if (reg) {
ast_copy_string(reg->file, file, sizeof(reg->file));
@@ -106,9 +108,9 @@ static inline void *__ast_alloc_region(size_t size, int which, const char *file,
}
ast_mutex_unlock(&reglock);
if (!reg) {
- fprintf(stderr, "Out of memory :(\n");
+ fprintf(stderr, "Memory allocation failure\n");
if (mmlog) {
- fprintf(mmlog, "%ld - Out of memory\n", time(NULL));
+ fprintf(mmlog, "%ld - Memory allocation failure\n", time(NULL));
fflush(mmlog);
}
}
@@ -184,8 +186,7 @@ static void __ast_free_region(void *ptr, const char *file, int lineno, const cha
void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
{
void *ptr;
- ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func);
- if (ptr)
+ if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func)))
memset(ptr, 0, size * nmemb);
return ptr;
}
@@ -204,19 +205,15 @@ void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const
{
void *tmp;
size_t len = 0;
- if (ptr) {
- len = __ast_sizeof_region(ptr);
- if (!len) {
- fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
- if (mmlog) {
- fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
- fflush(mmlog);
- }
- return NULL;
+ if (ptr && !(len = __ast_sizeof_region(ptr))) {
+ fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
+ if (mmlog) {
+ fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
+ fflush(mmlog);
}
+ return NULL;
}
- tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func);
- if (tmp) {
+ if ((tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func))) {
if (len > size)
len = size;
if (ptr) {
@@ -234,8 +231,7 @@ char *__ast_strdup(const char *s, const char *file, int lineno, const char *func
if (!s)
return NULL;
len = strlen(s) + 1;
- ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func);
- if (ptr)
+ if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func)))
strcpy(ptr, s);
return ptr;
}
@@ -249,8 +245,7 @@ char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const
len = strlen(s) + 1;
if (len > n)
len = n;
- ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func);
- if (ptr)
+ if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func)))
strcpy(ptr, s);
return ptr;
}
@@ -266,8 +261,7 @@ int __ast_asprintf(const char *file, int lineno, const char *func, char **strp,
va_copy(ap2, ap);
size = vsnprintf(&s, 1, fmt, ap2);
va_end(ap2);
- *strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func);
- if (!*strp) {
+ if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func))) {
va_end(ap);
return -1;
}
@@ -287,8 +281,7 @@ int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file,
va_copy(ap2, ap);
size = vsnprintf(&s, 1, fmt, ap2);
va_end(ap2);
- *strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func);
- if (!*strp) {
+ if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func))) {
va_end(ap);
return -1;
}
@@ -377,8 +370,8 @@ static int handle_show_memory_summary(int fd, int argc, char *argv[])
cur = cur->next;
}
if (!cur) {
- cur = alloca(sizeof(struct file_summary));
- memset(cur, 0, sizeof(struct file_summary));
+ cur = alloca(sizeof(*cur));
+ memset(cur, 0, sizeof(*cur));
ast_copy_string(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn));
cur->next = list;
list = cur;
diff --git a/autoservice.c b/autoservice.c
index 08cbb4734..0859a4ab1 100644
--- a/autoservice.c
+++ b/autoservice.c
@@ -107,23 +107,20 @@ int ast_autoservice_start(struct ast_channel *chan)
/* XXX if found, we return -1, why ??? */
/* If not, start autoservice on channel */
- if (!as) {
- as = calloc(1, sizeof(struct asent));
- if (as) {
- as->chan = chan;
- AST_LIST_INSERT_HEAD(&aslist, as, list);
- res = 0;
- if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
- if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
- ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
- /* There will only be a single member in the list at this point,
- the one we just added. */
- AST_LIST_REMOVE(&aslist, as, list);
- free(as);
- res = -1;
- } else
- pthread_kill(asthread, SIGURG);
- }
+ if (!as && (as = ast_calloc(1, sizeof(*as)))) {
+ as->chan = chan;
+ AST_LIST_INSERT_HEAD(&aslist, as, list);
+ res = 0;
+ if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
+ if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
+ ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
+ /* There will only be a single member in the list at this point,
+ the one we just added. */
+ AST_LIST_REMOVE(&aslist, as, list);
+ free(as);
+ res = -1;
+ } else
+ pthread_kill(asthread, SIGURG);
}
}
AST_LIST_UNLOCK(&aslist);
diff --git a/callerid.c b/callerid.c
index 161f5196d..c1137473f 100644
--- a/callerid.c
+++ b/callerid.c
@@ -131,32 +131,29 @@ void callerid_init(void)
struct callerid_state *callerid_new(int cid_signalling)
{
struct callerid_state *cid;
- cid = malloc(sizeof(struct callerid_state));
- if (cid) {
- memset(cid, 0, sizeof(struct callerid_state));
- cid->fskd.spb = 7; /* 1200 baud */
- cid->fskd.hdlc = 0; /* Async */
- cid->fskd.nbit = 8; /* 8 bits */
- cid->fskd.nstop = 1; /* 1 stop bit */
- cid->fskd.paridad = 0; /* No parity */
- cid->fskd.bw=1; /* Filter 800 Hz */
- if (cid_signalling == 2) { /* v23 signalling */
+
+ if ((cid = ast_calloc(1, sizeof(*cid)))) {
+ cid->fskd.spb = 7.0; /* 1200 baud */
+ /* cid->fskd.hdlc = 0; */ /* Async */
+ cid->fskd.nbit = 8; /* 8 bits */
+ cid->fskd.nstop = 1.0; /* 1 stop bit */
+ /* cid->fskd.paridad = 0; */ /* No parity */
+ cid->fskd.bw = 1; /* Filter 800 Hz */
+ if (cid_signalling == 2) { /* v23 signalling */
cid->fskd.f_mark_idx = 4; /* 1300 Hz */
cid->fskd.f_space_idx = 5; /* 2100 Hz */
- } else { /* Bell 202 signalling as default */
+ } else { /* Bell 202 signalling as default */
cid->fskd.f_mark_idx = 2; /* 1200 Hz */
cid->fskd.f_space_idx = 3; /* 2200 Hz */
}
- cid->fskd.pcola = 0; /* No clue */
- cid->fskd.cont = 0; /* Digital PLL reset */
- cid->fskd.x0 = 0.0;
- cid->fskd.state = 0;
- memset(cid->name, 0, sizeof(cid->name));
- memset(cid->number, 0, sizeof(cid->number));
+ /* cid->fskd.pcola = 0; */ /* No clue */
+ /* cid->fskd.cont = 0.0; */ /* Digital PLL reset */
+ /* cid->fskd.x0 = 0.0; */
+ /* cid->fskd.state = 0; */
cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
- cid->pos = 0;
- } else
- ast_log(LOG_WARNING, "Out of memory\n");
+ /* cid->pos = 0; */
+ }
+
return cid;
}
@@ -284,15 +281,14 @@ int callerid_feed_jp(struct callerid_state *cid, unsigned char *ubuf, int len, i
int b2 ;
int res;
int x;
- short *buf = malloc(2 * len + cid->oldlen);
- short *obuf = buf;
+ short *buf;
+ short *obuf;
- if (!buf) {
- ast_log(LOG_WARNING, "Out of memory\n");
+ if (!(buf = ast_calloc(1, 2 * len + cid->oldlen))) {
return -1;
}
- memset(buf, 0, 2 * len + cid->oldlen);
+ obuf = buf;
memcpy(buf, cid->oldstuff, cid->oldlen);
mylen += cid->oldlen/2;
@@ -534,15 +530,17 @@ int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int len, int
int b = 'X';
int res;
int x;
- short *buf = malloc(2 * len + cid->oldlen);
- short *obuf = buf;
- if (!buf) {
- ast_log(LOG_WARNING, "Out of memory\n");
+ short *buf;
+ short *obuf;
+
+ if (!(buf = ast_calloc(1, 2 * len + cid->oldlen))) {
return -1;
}
- memset(buf, 0, 2 * len + cid->oldlen);
+
+ obuf = buf;
memcpy(buf, cid->oldstuff, cid->oldlen);
mylen += cid->oldlen/2;
+
for (x=0;x<len;x++)
buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
while(mylen >= 160) {
diff --git a/cdr.c b/cdr.c
index 4e8d36795..29c2a5c3d 100644
--- a/cdr.c
+++ b/cdr.c
@@ -129,11 +129,9 @@ int ast_cdr_register(char *name, char *desc, ast_cdrbe be)
return -1;
}
- i = malloc(sizeof(*i));
- if (!i)
+ if (!(i = ast_calloc(1, sizeof(*i))))
return -1;
- memset(i, 0, sizeof(*i));
i->be = be;
ast_copy_string(i->name, name, sizeof(i->name));
ast_copy_string(i->desc, desc, sizeof(i->desc));
@@ -172,7 +170,6 @@ struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
struct ast_cdr *newcdr;
if (!(newcdr = ast_cdr_alloc())) {
- ast_log(LOG_ERROR, "Memory Error!\n");
return NULL;
}
@@ -438,13 +435,7 @@ void ast_cdr_free(struct ast_cdr *cdr)
struct ast_cdr *ast_cdr_alloc(void)
{
- struct ast_cdr *cdr;
-
- cdr = malloc(sizeof(*cdr));
- if (cdr)
- memset(cdr, 0, sizeof(*cdr));
-
- return cdr;
+ return ast_calloc(1, sizeof(struct ast_cdr));
}
void ast_cdr_start(struct ast_cdr *cdr)
@@ -876,9 +867,7 @@ static void reset_batch(void)
static int init_batch(void)
{
/* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
- batch = malloc(sizeof(*batch));
- if (!batch) {
- ast_log(LOG_WARNING, "CDR: out of memory while trying to handle batched records, data will most likely be lost\n");
+ if (!(batch = ast_malloc(sizeof(*batch)))) {
return -1;
}
@@ -986,15 +975,12 @@ void ast_cdr_detach(struct ast_cdr *cdr)
if (option_debug)
ast_log(LOG_DEBUG, "CDR detaching from this thread\n");
- /* we'll need a new tail for every CDR */
- newtail = malloc(sizeof(*newtail));
- if (!newtail) {
- ast_log(LOG_WARNING, "CDR: out of memory while trying to detach, will try in this thread instead\n");
+ /* we'll need a new tail for every CDR */
+ if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
post_cdr(cdr);
ast_cdr_free(cdr);
return;
}
- memset(newtail, 0, sizeof(*newtail));
/* don't traverse a whole list (just keep track of the tail) */
ast_mutex_lock(&cdr_batch_lock);
diff --git a/channel.c b/channel.c
index 7495e88b6..c5bff1255 100644
--- a/channel.c
+++ b/channel.c
@@ -417,10 +417,8 @@ int ast_channel_register(const struct ast_channel_tech *tech)
return -1;
}
}
-
- chan = malloc(sizeof(*chan));
- if (!chan) {
- ast_log(LOG_WARNING, "Out of memory\n");
+
+ if (!(chan = ast_malloc(sizeof(*chan)))) {
AST_LIST_UNLOCK(&channels);
return -1;
}
@@ -607,15 +605,11 @@ struct ast_channel *ast_channel_alloc(int needqueue)
return NULL;
}
- tmp = malloc(sizeof(struct ast_channel));
- if (!tmp) {
- ast_log(LOG_WARNING, "Channel allocation failed: Out of memory\n");
+ if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
return NULL;
}
- memset(tmp, 0, sizeof(struct ast_channel));
- tmp->sched = sched_context_create();
- if (!tmp->sched) {
+ if (!(tmp->sched = sched_context_create())) {
ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
free(tmp);
return NULL;
@@ -702,8 +696,7 @@ int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
int qlen = 0;
/* Build us a copy and free the original one */
- f = ast_frdup(fin);
- if (!f) {
+ if (!(f = ast_frdup(fin))) {
ast_log(LOG_WARNING, "Unable to duplicate frame\n");
return -1;
}
@@ -1048,8 +1041,7 @@ int ast_channel_spy_add(struct ast_channel *chan, struct ast_channel_spy *spy)
}
if (!chan->spies) {
- if (!(chan->spies = calloc(1, sizeof(*chan->spies)))) {
- ast_log(LOG_WARNING, "Memory allocation failure\n");
+ if (!(chan->spies = ast_calloc(1, sizeof(*chan->spies)))) {
return -1;
}
@@ -1478,9 +1470,8 @@ int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen,
}
ast_prod(chan);
- if (gen->alloc) {
- if (!(chan->generatordata = gen->alloc(chan, params)))
- res = -1;
+ if (gen->alloc && !(chan->generatordata = gen->alloc(chan, params))) {
+ res = -1;
}
if (!res) {
@@ -1520,9 +1511,7 @@ struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds,
} *fdmap;
sz = n * AST_MAX_FDS + nfds;
- pfds = alloca(sizeof(struct pollfd) * sz);
- fdmap = alloca(sizeof(struct fdmap) * sz);
- if (!pfds || !fdmap) {
+ if (!(pfds = alloca(sizeof(*pfds) * sz)) || !(fdmap = alloca(sizeof(*fdmap) * sz))) {
ast_log(LOG_ERROR, "Out of memory\n");
*outfd = -1;
return NULL;
@@ -2510,10 +2499,8 @@ struct ast_channel *__ast_request_and_dial(const char *type, int format, void *d
if (outstate)
*outstate = state;
if (chan && res <= 0) {
- if (!chan->cdr) {
- chan->cdr = ast_cdr_alloc();
- if (chan->cdr)
- ast_cdr_init(chan->cdr, chan);
+ if (!chan->cdr && (chan->cdr = ast_cdr_alloc())) {
+ ast_cdr_init(chan->cdr, chan);
}
if (chan->cdr) {
char tmp[256];
@@ -2525,8 +2512,7 @@ struct ast_channel *__ast_request_and_dial(const char *type, int format, void *d
/* If the cause wasn't handled properly */
if (ast_cdr_disposition(chan->cdr,chan->hangupcause))
ast_cdr_failed(chan->cdr);
- } else
- ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
+ }
ast_hangup(chan);
chan = NULL;
}
@@ -3663,8 +3649,7 @@ static void *tonepair_alloc(struct ast_channel *chan, void *params)
struct tonepair_state *ts;
struct tonepair_def *td = params;
- ts = calloc(1, sizeof(struct tonepair_state));
- if (!ts)
+ if (!(ts = ast_calloc(1, sizeof(*ts))))
return NULL;
ts->origwfmt = chan->writeformat;
if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
@@ -4074,8 +4059,7 @@ struct ast_silence_generator *ast_channel_start_silence_generator(struct ast_cha
{
struct ast_silence_generator *state;
- if (!(state = calloc(1, sizeof(*state)))) {
- ast_log(LOG_WARNING, "Could not allocate state structure\n");
+ if (!(state = ast_calloc(1, sizeof(*state)))) {
return NULL;
}
diff --git a/chanvars.c b/chanvars.c
index f2653b8ff..2c6bf00c0 100644
--- a/chanvars.c
+++ b/chanvars.c
@@ -33,23 +33,21 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/chanvars.h"
#include "asterisk/logger.h"
#include "asterisk/strings.h"
+#include "asterisk/utils.h"
struct ast_var_t *ast_var_assign(const char *name, const char *value)
-{
- int i;
+{
struct ast_var_t *var;
-
- var = calloc(sizeof(struct ast_var_t) + strlen(name) + 1 + strlen(value) + 1, sizeof(char));
+ int name_len = strlen(name) + 1;
+ int value_len = strlen(value) + 1;
- if (var == NULL) {
- ast_log(LOG_WARNING, "Out of memory\n");
+ if (!(var = ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char)))) {
return NULL;
}
- i = strlen(name) + 1;
- ast_copy_string(var->name, name, i);
- var->value = var->name + i;
- ast_copy_string(var->value, value, strlen(value) + 1);
+ ast_copy_string(var->name, name, name_len);
+ var->value = var->name + name_len;
+ ast_copy_string(var->value, value, value_len);
return var;
}