summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVadim Yanitskiy <axilirator@gmail.com>2017-09-09 12:10:08 +0300
committerVadim Yanitskiy <axilirator@gmail.com>2017-12-31 12:20:59 +0100
commit3c20dac38c774afe2dfb769451a1d987c21f9c65 (patch)
treef7d452ced8ebf76f1a99a584dd75c53a06028685
parenta4d88ae13bc60e3f6269334d633495668af0c640 (diff)
libosmogapk: use talloc for memory management
-rw-r--r--src/benchmark.c8
-rw-r--r--src/codec_amr.c5
-rw-r--r--src/codec_efr.c4
-rw-r--r--src/pq_alsa.c10
-rw-r--r--src/pq_file.c12
-rw-r--r--src/pq_rtp.c12
-rw-r--r--src/procqueue.c15
7 files changed, 36 insertions, 30 deletions
diff --git a/src/benchmark.c b/src/benchmark.c
index e9637b1..c523f55 100644
--- a/src/benchmark.c
+++ b/src/benchmark.c
@@ -17,9 +17,8 @@
* (C) 2014 Harald Welte <laforge@gnumonks.org>
*/
-#include <stdio.h>
+#include <talloc.h>
#include <errno.h>
-#include <stdlib.h>
#include <osmocom/gapk/benchmark.h>
#include <osmocom/gapk/codecs.h>
@@ -32,8 +31,7 @@ int osmo_gapk_bench_enable(enum osmo_gapk_codec_type codec)
struct osmo_gapk_bench_cycles *bench;
/* Allocate zero-initialized memory */
- bench = (struct osmo_gapk_bench_cycles *)
- calloc(1, sizeof(struct osmo_gapk_bench_cycles));
+ bench = talloc_zero(NULL, struct osmo_gapk_bench_cycles);
if (!bench)
return -ENOMEM;
@@ -84,5 +82,5 @@ void osmo_gapk_bench_free(void)
int i;
for (i = 0; i < _CODEC_MAX; i++)
- free(osmo_gapk_bench_codec[i]);
+ talloc_free(osmo_gapk_bench_codec[i]);
}
diff --git a/src/codec_amr.c b/src/codec_amr.c
index 2fa7a2f..df35b49 100644
--- a/src/codec_amr.c
+++ b/src/codec_amr.c
@@ -27,8 +27,7 @@
#ifdef HAVE_OPENCORE_AMRNB
-#include <stdlib.h>
-#include <stdio.h>
+#include <talloc.h>
#include <opencore-amrnb/interf_dec.h>
#include <opencore-amrnb/interf_enc.h>
@@ -44,7 +43,7 @@ codec_amr_init(void)
{
struct codec_amr_state *st;
- st = calloc(1, sizeof(*st));
+ st = talloc_zero(NULL, struct codec_amr_state);
if (!st)
return NULL;
diff --git a/src/codec_efr.c b/src/codec_efr.c
index f5b7f83..98bfdcd 100644
--- a/src/codec_efr.c
+++ b/src/codec_efr.c
@@ -26,7 +26,7 @@
#ifdef HAVE_OPENCORE_AMRNB
-#include <stdlib.h>
+#include <talloc.h>
#include <assert.h>
#include <opencore-amrnb/interf_dec.h>
@@ -44,7 +44,7 @@ codec_efr_init(void)
{
struct codec_efr_state *st;
- st = calloc(1, sizeof(*st));
+ st = talloc_zero(NULL, struct codec_efr_state);
if (!st)
return NULL;
diff --git a/src/pq_alsa.c b/src/pq_alsa.c
index b434b5b..8355349 100644
--- a/src/pq_alsa.c
+++ b/src/pq_alsa.c
@@ -20,8 +20,7 @@
#include <errno.h>
#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <talloc.h>
#include <osmocom/gapk/logging.h>
#include <osmocom/gapk/codecs.h>
@@ -92,7 +91,7 @@ pq_queue_alsa_op(struct osmo_gapk_pq *pq, const char *alsa_dev, unsigned int blk
snd_pcm_hw_params_t *hw_params;
int rc = -1;
- state = calloc(1, sizeof(struct pq_state_alsa));
+ state = talloc_zero(pq, struct pq_state_alsa);
if (!state) {
rc = -ENOMEM;
goto out_print;
@@ -148,13 +147,16 @@ pq_queue_alsa_op(struct osmo_gapk_pq *pq, const char *alsa_dev, unsigned int blk
item->wait = pq_cb_alsa_wait;
item->exit = pq_cb_alsa_exit;
+ /* Change state's talloc context from pq to item */
+ talloc_steal(item, state);
+
return 0;
out_free_par:
snd_pcm_hw_params_free(hw_params);
out_close:
snd_pcm_close(state->pcm_handle);
- free(state);
+ talloc_free(state);
out_print:
LOGPGAPK(LOGL_ERROR, "Couldn't init ALSA device '%s': %s\n",
alsa_dev, snd_strerror(rc));
diff --git a/src/pq_file.c b/src/pq_file.c
index d05f82a..f3bb9a3 100644
--- a/src/pq_file.c
+++ b/src/pq_file.c
@@ -19,8 +19,7 @@
#include <errno.h>
#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <talloc.h>
#include <osmocom/gapk/logging.h>
#include <osmocom/gapk/codecs.h>
@@ -57,7 +56,7 @@ pq_cb_file_output(void *_state, uint8_t *out, const uint8_t *in, unsigned int in
static void
pq_cb_file_exit(void *_state)
{
- free(_state);
+ talloc_free(_state);
}
static int
@@ -66,7 +65,7 @@ pq_queue_file_op(struct osmo_gapk_pq *pq, FILE *fh, unsigned int blk_len, int in
struct osmo_gapk_pq_item *item;
struct pq_state_file *state;
- state = calloc(1, sizeof(struct pq_state_file));
+ state = talloc_zero(pq, struct pq_state_file);
if (!state)
return -ENOMEM;
@@ -75,7 +74,7 @@ pq_queue_file_op(struct osmo_gapk_pq *pq, FILE *fh, unsigned int blk_len, int in
item = osmo_gapk_pq_add_item(pq);
if (!item) {
- free(state);
+ talloc_free(state);
return -ENOMEM;
}
@@ -86,6 +85,9 @@ pq_queue_file_op(struct osmo_gapk_pq *pq, FILE *fh, unsigned int blk_len, int in
item->wait = NULL;
item->exit = pq_cb_file_exit;
+ /* Change state's talloc context from pq to item */
+ talloc_steal(item, state);
+
return 0;
}
diff --git a/src/pq_rtp.c b/src/pq_rtp.c
index 1c37475..27b868c 100644
--- a/src/pq_rtp.c
+++ b/src/pq_rtp.c
@@ -20,10 +20,9 @@
#include <errno.h>
#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <unistd.h>
#include <string.h>
+#include <talloc.h>
#include <arpa/inet.h>
@@ -190,7 +189,7 @@ pq_cb_rtp_output(void *_state, uint8_t *out, const uint8_t *in, unsigned int in_
static void
pq_cb_rtp_exit(void *_state)
{
- free(_state);
+ talloc_free(_state);
}
static int
@@ -199,7 +198,7 @@ pq_queue_rtp_op(struct osmo_gapk_pq *pq, int udp_fd, unsigned int blk_len, int i
struct osmo_gapk_pq_item *item;
struct pq_state_rtp *state;
- state = calloc(1, sizeof(struct pq_state_rtp));
+ state = talloc_zero(pq, struct pq_state_rtp);
if (!state)
return -ENOMEM;
@@ -221,7 +220,7 @@ pq_queue_rtp_op(struct osmo_gapk_pq *pq, int udp_fd, unsigned int blk_len, int i
item = osmo_gapk_pq_add_item(pq);
if (!item) {
- free(state);
+ talloc_free(state);
return -ENOMEM;
}
@@ -232,6 +231,9 @@ pq_queue_rtp_op(struct osmo_gapk_pq *pq, int udp_fd, unsigned int blk_len, int i
item->wait = NULL;
item->exit = pq_cb_rtp_exit;
+ /* Change state's talloc context from pq to item */
+ talloc_steal(item, state);
+
return 0;
}
diff --git a/src/procqueue.c b/src/procqueue.c
index ffd3f6d..dcf55bc 100644
--- a/src/procqueue.c
+++ b/src/procqueue.c
@@ -20,6 +20,7 @@
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
+#include <talloc.h>
#include <osmocom/core/linuxlist.h>
@@ -33,7 +34,9 @@ osmo_gapk_pq_create(void)
struct osmo_gapk_pq *pq;
/* Allocate memory for a new processing queue */
- pq = (struct osmo_gapk_pq *) calloc(1, sizeof(struct osmo_gapk_pq));
+ pq = talloc_zero(NULL, struct osmo_gapk_pq);
+ if (!pq)
+ return NULL;
/* Init its list of items */
INIT_LLIST_HEAD(&pq->items);
@@ -54,7 +57,7 @@ osmo_gapk_pq_destroy(struct osmo_gapk_pq *pq)
/* Iterate over all items in queue */
llist_for_each_entry_safe(item, item_next, &pq->items, list) {
/* Free output buffer memory */
- free(item->buf);
+ talloc_free(item->buf);
/* Call exit handler if preset */
if (item->exit)
@@ -62,10 +65,10 @@ osmo_gapk_pq_destroy(struct osmo_gapk_pq *pq)
/* Delete an item from list */
llist_del(&item->list);
- free(item);
+ talloc_free(item);
}
- free(pq);
+ talloc_free(pq);
}
/*! allocate + add an item to a processing queue; return new item
@@ -77,7 +80,7 @@ osmo_gapk_pq_add_item(struct osmo_gapk_pq *pq)
struct osmo_gapk_pq_item *item;
/* Allocate memory for a new item */
- item = calloc(1, sizeof(struct osmo_gapk_pq_item));
+ item = talloc_zero(pq, struct osmo_gapk_pq_item);
if (!item)
return NULL;
@@ -120,7 +123,7 @@ osmo_gapk_pq_prepare(struct osmo_gapk_pq *pq)
buf_size = VAR_BUF_SIZE;
/* Allocate memory for an output buffer */
- item->buf = malloc(buf_size);
+ item->buf = talloc_size(item, buf_size);
if (!item->buf)
return -ENOMEM;
} else {