Browse Source

Create helper functions for aligning all memory allocations and checking their return values and make dealloc take a pointer as a macro

master
Con Kolivas 11 years ago
parent
commit
76495c07fc
  1. 49
      src/libckpool.c
  2. 7
      src/libckpool.h

49
src/libckpool.c

@ -259,15 +259,10 @@ bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port)
} }
hlen = url_len + 1; hlen = url_len + 1;
align_len(&hlen); url_address = ckalloc(hlen);
url_address = malloc(hlen);
if (unlikely(!url_address))
quit(1, "Failed to malloc url_address of length %d in extract_sockaddr", (int)hlen);
sprintf(url_address, "%.*s", url_len, url_begin); sprintf(url_address, "%.*s", url_len, url_begin);
port = malloc(8); port = ckalloc(8);
if (unlikely(!port))
quit(1, "Failed to malloc port in extract_sockaddr");
if (port_len) { if (port_len) {
char *slash; char *slash;
@ -424,7 +419,7 @@ int read_socket_line(connsock_t *cs)
int ret, bufsiz; int ret, bufsiz;
fd_set rd; fd_set rd;
dealloc(&cs->buf); dealloc(cs->buf);
retry: retry:
FD_ZERO(&rd); FD_ZERO(&rd);
FD_SET(cs->fd, &rd); FD_SET(cs->fd, &rd);
@ -471,7 +466,7 @@ retry:
ret = bufofs + 1; ret = bufofs + 1;
out: out:
if (ret < 1) if (ret < 1)
dealloc(&cs->buf); dealloc(cs->buf);
return ret; return ret;
} }
@ -564,7 +559,7 @@ json_t *json_rpc_call(connsock_t *cs, const char *rpc_req)
if (!val) if (!val)
LOGWARNING("JSON decode failed(%d): %s", err_val.line, err_val.text); LOGWARNING("JSON decode failed(%d): %s", err_val.line, err_val.text);
out: out:
dealloc(&cs->buf); dealloc(cs->buf);
return val; return val;
} }
@ -602,6 +597,28 @@ void realloc_strcat(char **ptr, const char *s)
sprintf(ofs, "%s", s); sprintf(ofs, "%s", s);
} }
void *_ckalloc(size_t len, const char *file, const char *func, const int line)
{
void *ptr;
align_len(&len);
ptr = malloc(len);
if (unlikely(!ptr))
quitfrom(1, file, func, line, "Failed to ckalloc! errno=%d", errno);
return ptr;
}
void *_ckzalloc(size_t len, const char *file, const char *func, const int line)
{
void *ptr;
align_len(&len);
ptr = calloc(len, 1);
if (unlikely(!ptr))
quitfrom(1, file, func, line, "Failed to ckalloc! errno=%d", errno);
return ptr;
}
void _dealloc(void **ptr) void _dealloc(void **ptr)
{ {
free(*ptr); free(*ptr);
@ -630,14 +647,9 @@ void *bin2hex(const uchar *p, size_t len)
uchar *s; uchar *s;
slen = len * 2 + 1; slen = len * 2 + 1;
align_len(&slen); s = ckzalloc(slen);
s = calloc(slen, 1);
if (unlikely(!s)) {
LOGERR("Failed to calloc s in bin2hex");
} else
__bin2hex(s, p, len); __bin2hex(s, p, len);
/* Returns NULL if calloc failed. */
return s; return s;
} }
@ -742,10 +754,7 @@ char *http_base64(const char *src)
l = strlen((const char *)src); l = strlen((const char *)src);
hlen = ((l + 2) / 3) * 4 + 1; hlen = ((l + 2) / 3) * 4 + 1;
align_len(&hlen); str = ckalloc(hlen);
str = malloc(hlen);
if (unlikely(!str))
quit(1, "Failed to malloc string of length %d in http_base64", (int)hlen);
dst = str; dst = str;
r = 0; r = 0;

7
src/libckpool.h

@ -45,7 +45,10 @@
#define ck_runlock(_lock) _ck_runlock(_lock, __FILE__, __func__, __LINE__) #define ck_runlock(_lock) _ck_runlock(_lock, __FILE__, __func__, __LINE__)
#define ck_wunlock(_lock) _ck_wunlock(_lock, __FILE__, __func__, __LINE__) #define ck_wunlock(_lock) _ck_wunlock(_lock, __FILE__, __func__, __LINE__)
#define dealloc(ptr) _dealloc((void *)(ptr)) #define ckalloc(len) _ckalloc(len, __FILE__, __func__, __LINE__)
#define ckzalloc(len) _ckzalloc(len, __FILE__, __func__, __LINE__)
#define dealloc(ptr) _dealloc((void *)&(ptr))
/* Placeholders for when we have more comprehensive logging facilities */ /* Placeholders for when we have more comprehensive logging facilities */
#define LOGERR(fmt, ...) do { \ #define LOGERR(fmt, ...) do { \
@ -189,6 +192,8 @@ json_t *json_rpc_call(connsock_t *cs, const char *rpc_req);
void align_len(size_t *len); void align_len(size_t *len);
void realloc_strcat(char **ptr, const char *s); void realloc_strcat(char **ptr, const char *s);
void *_ckalloc(size_t len, const char *file, const char *func, const int line);
void *_ckzalloc(size_t len, const char *file, const char *func, const int line);
void _dealloc(void **ptr); void _dealloc(void **ptr);
void __bin2hex(uchar *s, const uchar *p, size_t len); void __bin2hex(uchar *s, const uchar *p, size_t len);
void *bin2hex(const uchar *p, size_t len); void *bin2hex(const uchar *p, size_t len);

Loading…
Cancel
Save