Commit 35bb5d28 authored by Jenda's avatar Jenda

error checking

parent 41d19841
......@@ -43,7 +43,7 @@ void msg_running_xlater(tcp_cli_t * me, worker * w) {
// First send the new xlater info with provided ID to the originating client
size_t len = c2s__srv__running__xlater__get_packed_size(&s);
void * buf = malloc(len);
void * buf = safe_malloc(len);
c2s__srv__running__xlater__pack(&s, buf);
uint32_t size = len + sizeof(int32_t); LE32(&size);
......@@ -96,7 +96,7 @@ void msg_server_info(tcp_cli_t * me, bool toall) {
s.maxtaps = MAXTAPS;
size_t len = c2s__srv__info__get_packed_size(&s);
void * buf = malloc(len);
void * buf = safe_malloc(len);
c2s__srv__info__pack(&s, buf);
uint32_t size = len + sizeof(int32_t); LE32(&size);
......@@ -128,7 +128,7 @@ void msg_destroyed_xlater(int32_t xid) {
s.id = xid;
size_t len = c2s__srv__destroyed__xlater__get_packed_size(&s);
void * buf = malloc(len);
void * buf = safe_malloc(len);
c2s__srv__destroyed__xlater__pack(&s, buf);
uint32_t size = len + sizeof(int32_t); LE32(&size);
......@@ -157,7 +157,7 @@ int parse_client_req(tcp_cli_t * me, const uint8_t * buf2, int32_t len) {
s = c2s__cli__create__xlater__unpack(NULL, len, buf);
size_t tapslen = s->n_taps;
float * taps = malloc(tapslen * sizeof(float));
float * taps = safe_malloc(tapslen * sizeof(float));
memcpy(taps, s->taps, tapslen * sizeof(float));
pthread_mutex_lock(&llmutex);
......@@ -176,7 +176,7 @@ int parse_client_req(tcp_cli_t * me, const uint8_t * buf2, int32_t len) {
pthread_mutex_lock(&llmutex);
req_frames * r = malloc(sizeof(req_frames));
req_frames * r = safe_malloc(sizeof(req_frames));
r->wid = s->id;
r->sampletype = s->type;
SLIST_INSERT_HEAD(&(me->req_frames_head), r, next);
......@@ -224,7 +224,7 @@ int parse_client_req(tcp_cli_t * me, const uint8_t * buf2, int32_t len) {
SLIST_FOREACH(w, &worker_head, next) {
if(w->wid == s->localid) {
if(tapslen != 0) {
float * taps = malloc(tapslen * sizeof(float));
float * taps = safe_malloc(tapslen * sizeof(float));
memcpy(taps, s->newtaps, tapslen * sizeof(float));
w->newtaps = taps;
w->newtapslen = tapslen;
......@@ -314,7 +314,7 @@ int parse_client_req(tcp_cli_t * me, const uint8_t * buf2, int32_t len) {
rec_stop = s->stopframe;
if(asprintf(&recpath, "rec-%li-%i", time(0), samplerate) == -1) {
err(1, "asprintf");
err(EXIT_FAILURE, "asprintf");
}
pthread_mutex_unlock(&datamutex);
......
......@@ -14,7 +14,7 @@ freq="$(( 100 * 1000 * 1000 ))"
[ ! -p "$sdr" ] && mkfifo "$sdr"
[ ! -p "$tune" ] && mkfifo "$tune"
./server -s 2048000 -p "$ppm" -f "$freq" -i "$tune" -o "$sdr" -f "$freq" -g "$gain" -r "$2" -w 1024 &
./server -s 2048000 -p "$ppm" -f "$freq" -i "$tune" -o "$sdr" -f "$freq" -g "$gain" -s "$2" -w 1024 &
spid=$!
echo "Server PID $spid, use 'gdb ./server $spid -ex c' to debug"
......
......@@ -10,6 +10,7 @@
#include "metadata.h"
#include "sdr_packet.h"
#include "constants.h"
#include "util.h"
int fftsize;
float* fftw_window;
......@@ -36,8 +37,8 @@ void fftw_init(int N) {
fftwf_free(fftw_out);
}
fftw_in = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * N);
fftw_out = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * N);
fftw_in = (fftwf_complex*) fftwf_safe_malloc(sizeof(fftwf_complex) * N);
fftw_out = (fftwf_complex*) fftwf_safe_malloc(sizeof(fftwf_complex) * N);
p = fftwf_plan_dft_1d(N, fftw_in, fftw_out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_window = realloc(fftw_window, sizeof(float) * N);
......@@ -52,7 +53,7 @@ void fftw_init(int N) {
void calc_spectrum(sdr_packet * pkt, int spp, int fftskip) {
if(fftw_window == NULL) {
err(1, "Internal consistency, calling calc_spectrum before fftw_init");
err(EXIT_FAILURE, "Internal consistency, calling calc_spectrum before fftw_init");
}
size_t packet_len = spp * fftsize * sizeof(float);
......
......@@ -68,10 +68,10 @@ SLIST_HEAD(worker_head_t, worker) worker_head = SLIST_HEAD_INITIALIZER(worker_he
static void allocate_sdr_buf() {
size_t align = volk_get_alignment();
for(int i = 0; i<BUFSIZE; i++) {
sdr_inbuf[i].data = volk_malloc(COMPLEX * sizeof(float) * SDRPACKETSIZE, align);
sdr_inbuf[i].histo = malloc(sizeof(uint16_t) * HISTOGRAM_RES);
sdr_inbuf[i].data = volk_safe_malloc(COMPLEX * sizeof(float) * SDRPACKETSIZE, align);
sdr_inbuf[i].histo = safe_malloc(sizeof(uint16_t) * HISTOGRAM_RES);
if(!sdr_inbuf[i].data) {
err(1, "Cannot allocate SDR input buffer");
err(EXIT_FAILURE, "Cannot allocate SDR input buffer");
}
}
}
......@@ -99,10 +99,10 @@ static void * sdr_read_thr(void * a) {
char * metapath;
if(asprintf(&cfilepath, "%s.cfile", recpath) == -1) {
err(1, "asprintf");
err(EXIT_FAILURE, "asprintf");
}
if(asprintf(&metapath, "%s.txt", recpath) == -1) {
err(1, "asprintf");
err(EXIT_FAILURE, "asprintf");
}
FILE * cfile = fopen(cfilepath, "ab");
......@@ -122,7 +122,7 @@ static void * sdr_read_thr(void * a) {
p->frequency);
if(ret == -1) {
err(1, "asprintf");
err(EXIT_FAILURE, "asprintf");
}
fwrite(metaline, strlen(metaline), 1, metafile);
......@@ -167,7 +167,7 @@ static void * sdr_read_thr(void * a) {
size_t r = fread(sdr_inbuf[base].data, COMPLEX*sizeof(float), SDRPACKETSIZE, sdr_pipe);
if(r != SDRPACKETSIZE) {
err(1, "short read from sdr at frame %i (%zu, %i)\n", sdr_cptr, r, SDRPACKETSIZE);
err(EXIT_FAILURE, "short read from sdr at frame %i (%zu, %i)\n", sdr_cptr, r, SDRPACKETSIZE);
}
sdr_inbuf[base].timestamp = time(0);
sdr_inbuf[base].frameno = sdr_cptr;
......@@ -360,13 +360,13 @@ static void create_read_write_threads() {
int ret = pthread_create(&sdr_thread, NULL, &sdr_read_thr, NULL);
if(ret != 0) {
err(1, "Cannot create SDR thread!\n");
err(EXIT_FAILURE, "Cannot create SDR thread!\n");
}
pthread_setname_np(sdr_thread, "sdr_read_thr");
ret = pthread_create(&socket_thread, NULL, &socket_write_thr, NULL);
if(ret != 0) {
err(1, "Cannot create socket thread!\n");
err(EXIT_FAILURE, "Cannot create socket thread!\n");
}
pthread_setname_np(socket_thread, "socket_write_t");
......@@ -431,7 +431,7 @@ int main(int argc, char **argv) {
sigaddset(&set, SIGPIPE);
int s = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (s != 0)
err(1, "pthread_sigmask");
err(EXIT_FAILURE, "pthread_sigmask");
fftw_init(fftw);
......
......@@ -48,7 +48,7 @@ int bind_me(char * port, const char * address, bool wildcard) {
}
if (s != 0) {
err(1, "getaddrinfo: %s\n", gai_strerror(s));
err(EXIT_FAILURE, "getaddrinfo: %s\n", gai_strerror(s));
}
/* getaddrinfo() returns a list of address structures.
......@@ -59,7 +59,7 @@ int bind_me(char * port, const char * address, bool wildcard) {
for (rp = result; rp != NULL; rp = rp->ai_next) {
if (rp->ai_family == AF_INET6) {
char * buf = malloc(INET6_ADDRSTRLEN);
char * buf = safe_malloc(INET6_ADDRSTRLEN);
struct sockaddr_in * saddr = (struct sockaddr_in *)
rp->ai_addr;
if (inet_ntop(AF_INET6, &(saddr->sin_addr), buf,
......@@ -70,7 +70,7 @@ int bind_me(char * port, const char * address, bool wildcard) {
}
free(buf);
} else if (rp->ai_family == AF_INET) {
char * buf = malloc(INET_ADDRSTRLEN);
char * buf = safe_malloc(INET_ADDRSTRLEN);
struct sockaddr_in * saddr = (struct sockaddr_in *)
rp->ai_addr;
if (inet_ntop(AF_INET, & (saddr->sin_addr), buf,
......@@ -81,7 +81,7 @@ int bind_me(char * port, const char * address, bool wildcard) {
}
free(buf);
} else if (rp->ai_family == AF_INET) {
char * buf = malloc(INET_ADDRSTRLEN);
char * buf = safe_malloc(INET_ADDRSTRLEN);
struct sockaddr_in * saddr = (struct sockaddr_in *)
rp->ai_addr;
if (inet_ntop(AF_INET, & (saddr->sin_addr), buf,
......@@ -122,7 +122,7 @@ int bind_me(char * port, const char * address, bool wildcard) {
}
if (rp == NULL) { /* No address succeeded */
err(1, "Could not bind\n");
err(EXIT_FAILURE, "Could not bind\n");
}
freeaddrinfo(result); /* No longer needed */
......@@ -150,7 +150,7 @@ void * client_read_thr(void * param) {
fprintf(stderr, "Read garbage size: %i\n", size);
break;
}
uint8_t * buf = (uint8_t*) malloc(size);
uint8_t * buf = (uint8_t*) safe_malloc(size);
ret = readn(me->fd, buf, size);
if(ret < size) {
free(buf);
......@@ -192,7 +192,7 @@ void network_listener(char * host, char * port) {
}
pthread_t thread_id;
tcp_cli_t * cli = malloc(sizeof(tcp_cli_t));
tcp_cli_t * cli = safe_malloc(sizeof(tcp_cli_t));
cli->fd = newsockfd;
pthread_mutex_lock(&llmutex);
......@@ -202,7 +202,7 @@ void network_listener(char * host, char * port) {
int ret = pthread_create(&thread_id, NULL, &client_read_thr, (void*)cli);
if(ret < 0) {
err(1, "Cannot create client thread");
err(EXIT_FAILURE, "Cannot create client thread");
}
pthread_setname_np(thread_id, "client_read_t");
......
#include "util.h"
#include <stddef.h>
#include <unistd.h>
#include <unistd.h>
#include <fftw3.h>
ssize_t readn(int fd, void * buf2, int n) {
char * buf = (char*)buf2;
......@@ -26,3 +28,28 @@ ssize_t writen(int fd, void * buf2, int n) {
}
return pos;
}
void* safe_malloc(size_t size) {
void* p = malloc(size);
if (!p) {
err(EXIT_FAILURE, "malloc");
}
return p;
}
void* volk_safe_malloc(size_t size, size_t align) {
void* p = volk_malloc(size, align);
if (!p) {
err(EXIT_FAILURE, "volk_malloc");
}
return p;
}
void* fftwf_safe_malloc(size_t size) {
void* p = fftwf_malloc(size);
if (!p) {
err(EXIT_FAILURE, "volk_malloc");
}
return p;
}
......@@ -2,7 +2,14 @@
#ifndef UTIL_H
#define UTIL_H
#include <err.h>
#include <volk/volk.h>
ssize_t readn(int, void *, int);
ssize_t writen(int, void *, int);
void* safe_malloc(size_t size);
void* volk_safe_malloc(size_t size, size_t align);
void* fftwf_safe_malloc(size_t size);
#endif
......@@ -18,6 +18,7 @@
#include "worker.h"
#include "xlate_worker.h"
#include "sdr_packet.h"
#include "util.h"
extern int32_t sdr_cptr;
extern int32_t send_cptr;
......@@ -43,7 +44,7 @@ float calc_max_amplitude(float * taps, int tapslen) {
float * get_complex_taps(float * taps, int tapslen, float rotate) {
size_t align = volk_get_alignment();
float * ctaps = volk_malloc(tapslen * COMPLEX * sizeof(float), align);
float * ctaps = volk_safe_malloc(tapslen * COMPLEX * sizeof(float), align);
for(int i = 0; i<tapslen; i++) {
ctaps[COMPLEX*i] = taps[i] * cos(rotate*i);
......@@ -80,14 +81,14 @@ worker * create_xlate_worker(float rotate, int decim, int history, float * taps,
w->maxoutsize = COMPLEX * sizeof(float) * SDRPACKETSIZE/decim;
size_t align = volk_get_alignment();
//workers[wid].outbuf = malloc(sizeof(char*) * BUFSIZE);
//workers[wid].outbuf = safe_malloc(sizeof(char*) * BUFSIZE);
for(int i = 0; i<BUFSIZE; i++) {
w->outbuf[i].data = volk_malloc(w->maxoutsize, align);
w->outbuf[i].data = volk_safe_malloc(w->maxoutsize, align);
}
int ret = pthread_create(&w->thr, NULL, &xlate_worker_thr, (void*) w);
if(ret < 0) {
err(1, "cannot create xlater worker thread");
err(EXIT_FAILURE, "cannot create xlater worker thread");
}
pthread_setname_np(w->thr, "worker");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment