sanitized emalloc into emallocz which let's us get rid of various init crap

This commit is contained in:
garbeam 2005-12-08 02:31:23 +02:00
parent 244f66731a
commit 629b23ae5d
21 changed files with 57 additions and 100 deletions

View File

@ -9,15 +9,12 @@
#include "wm.h"
static Area zero_area = {0};
Area *alloc_area(Page *p, XRectangle * r, char *layout)
{
char buf[MAX_BUF];
Area *a = (Area *) cext_emalloc(sizeof(Area));
Area *a = (Area *) cext_emallocz(sizeof(Area));
size_t id = cext_sizeof(&p->areas);
*a = zero_area;
a->rect = *r;
a->page = p;
snprintf(buf, MAX_BUF, "/%s/a/%d", p->file[P_PREFIX]->name, id);

View File

@ -10,16 +10,13 @@
#include "wm.h"
static Client zero_client = { 0 };
Client *alloc_client(Window w)
{
static int id = 0;
char buf[MAX_BUF];
char buf2[MAX_BUF];
Client *c = (Client *) cext_emalloc(sizeof(Client));
Client *c = (Client *) cext_emallocz(sizeof(Client));
*c = zero_client;
c->win = w;
snprintf(buf, MAX_BUF, "/detached/c/%d", id);
c->file[C_PREFIX] = ixp_create(ixps, buf);

View File

@ -10,8 +10,6 @@
#include "wm.h"
static Frame zero_frame = { 0 };
static void select_client(void *obj, char *cmd);
static void handle_after_write_frame(IXPServer * s, File * f);
static void handle_before_read_frame(IXPServer * s, File * f);
@ -27,10 +25,9 @@ Frame *alloc_frame(XRectangle * r)
XSetWindowAttributes wa;
static int id = 0;
char buf[MAX_BUF];
Frame *f = (Frame *) cext_emalloc(sizeof(Frame));
Frame *f = (Frame *) cext_emallocz(sizeof(Frame));
int bw, th;
*f = zero_frame;
f->rect = *r;
f->cursor = normal_cursor;

View File

@ -34,9 +34,6 @@ static void resize_col(Frame * f, XRectangle * new, XPoint * pt);
static Layout lcol = { "col", init_col, deinit_col, arrange_col, attach_col, detach_col, resize_col };
static Column zero_column = { 0 };
static Acme zero_acme = { 0 };
void init_layout_column()
{
cext_attach_item(&layouts, &lcol);
@ -76,13 +73,12 @@ static void arrange_col(Area *a)
static void init_col(Area *a)
{
Acme *acme = cext_emalloc(sizeof(Acme));
Acme *acme = cext_emallocz(sizeof(Acme));
int i, cols = 1;
unsigned int width;
//size_t size;
Column *col;
*acme = zero_acme;
a->aux = acme;
/* processing argv */
@ -100,8 +96,7 @@ static void init_col(Area *a)
width = a->rect.width / cols;
for (i = 0; i < cols; i++) {
col = cext_emalloc(sizeof(Column));
*col = zero_column;
col = cext_emallocz(sizeof(Column));
col->rect = a->rect;
col->rect.x = i * width;
col->rect.width = width;

View File

@ -37,8 +37,7 @@ static void iter_attach_float(void *client, void *area)
static void init_float(Area *a)
{
Container *c = cext_emalloc(sizeof(Container));
c->list = c->stack = nil;
Container *c = cext_emallocz(sizeof(Container));
a->aux = c;
cext_iterate(&a->clients, a, iter_attach_float);
}

View File

@ -20,7 +20,7 @@ Action page_acttbl[] = {
Page *alloc_page()
{
Page *p = cext_emalloc(sizeof(Page));
Page *p = cext_emallocz(sizeof(Page));
char buf[MAX_BUF], buf2[16];
size_t id = cext_sizeof(&pages);
@ -98,7 +98,7 @@ XRectangle *rectangles(unsigned int *num)
XRectangle r;
if (XQueryTree(dpy, root, &d1, &d2, &wins, num)) {
result = cext_emalloc(*num * sizeof(XRectangle));
result = cext_emallocz(*num * sizeof(XRectangle));
for (i = 0; i < *num; i++) {
if (!XGetWindowAttributes(dpy, wins[i], &wa))
continue;

View File

@ -288,14 +288,14 @@ static void draw_bar(void *obj, char *arg)
n = 0;
for (f = label; f; f = f->next)
n++;
paths = cext_emalloc(sizeof(char *) * n);
paths = cext_emallocz(sizeof(char *) * n);
i = 0;
for (f = label; f; f = f->next)
paths[i++] = f->name;
qsort(paths, n, sizeof(char *), comp_str);
for (i = 0; i < n; i++) {
snprintf(buf, sizeof(buf), "/%s", paths[i]);
item = cext_emalloc(sizeof(Item));
item = cext_emallocz(sizeof(Item));
items = (Item **) attach_item_end((void **) items, item, sizeof(Item *));
init_item(buf, item);
}

View File

@ -207,13 +207,11 @@ static int make_qid(Qid * dir, char *wname, Qid * new)
static int attach(IXPServer * s, IXPConn * c)
{
Map *map = cext_emalloc(sizeof(Map));
fprintf(stderr, "attaching %d %s %s\n", s->fcall.afid, s->fcall.uname,
s->fcall.aname);
Map *map = cext_emallocz(sizeof(Map));
fprintf(stderr, "attaching %d %s %s\n", s->fcall.afid, s->fcall.uname, s->fcall.aname);
map->qid = root_qid;
map->fid = s->fcall.fid;
c->aux =
(Map **) attach_item_begin((void **) c->aux, map, sizeof(Map *));
c->aux = (Map **) attach_item_begin((void **) c->aux, map, sizeof(Map *));
s->fcall.id = RATTACH;
s->fcall.qid = root_qid;
return TRUE;
@ -252,16 +250,13 @@ static int walk(IXPServer * s, IXPConn * c)
*/
if (nwqid == s->fcall.nwname) {
if (s->fcall.fid == s->fcall.newfid) {
c->aux =
(Map **) detach_item((void **) c->aux, map, sizeof(Map *));
c->aux = (Map **) detach_item((void **) c->aux, map, sizeof(Map *));
free(map);
}
map = cext_emalloc(sizeof(Map));
map = cext_emallocz(sizeof(Map));
map->qid = qid;
map->fid = s->fcall.newfid;
c->aux =
(Map **) attach_item_begin((void **) c->aux, map,
sizeof(Map *));
c->aux = (Map **) attach_item_begin((void **) c->aux, map, sizeof(Map *));
}
s->fcall.id = RWALK;
s->fcall.nwqid = nwqid;
@ -437,7 +432,7 @@ int main(int argc, char *argv[])
atexit(exit_cleanup);
/* default item settings */
item = cext_emalloc(sizeof(Item));
item = cext_emallocz(sizeof(Item));
item->id = 0;
item->text[0] = '\0';
item->value = 0;

View File

@ -29,7 +29,6 @@ struct Bind {
char *prefix;
};
static Bind zero_bind = { 0 };
static Display *dpy;
static IXPServer *ixps;
static char *sockfile = 0;
@ -135,9 +134,7 @@ static void bind(void *obj, char *arg)
arg);
return; /* shortcut with empty argument */
}
b = cext_emalloc(sizeof(Bind));
*b = zero_bind;
b = cext_emallocz(sizeof(Bind));
b->client = init_ixp_client(sfile);
if (!b->client) {

View File

@ -54,8 +54,6 @@ static unsigned int num_lock_mask, valid_mask;
static char buf[MAX_BUF];
static XFontStruct *font;
static Shortcut zero_shortcut = { "", 0, 0, 0, 0 };
static void grab_shortcut(Shortcut * s);
static void ungrab_shortcut(Shortcut * s);
static void draw_shortcut_box(char *text);
@ -126,12 +124,11 @@ static void create_shortcut(File * f)
for (i = 0; i < toks; i++) {
if (!s)
r = s = cext_emalloc(sizeof(Shortcut));
r = s = cext_emallocz(sizeof(Shortcut));
else {
s->next = cext_emalloc(sizeof(Shortcut));
s->next = cext_emallocz(sizeof(Shortcut));
s = s->next;
}
*s = zero_shortcut;
cext_strlcpy(s->name, chain[i], MAX_BUF);
k = strrchr(chain[i], '-');
if (k)

View File

@ -111,9 +111,8 @@ static void _exec(char *cmd)
add_history(cmd);
if (files[M_PRE_COMMAND]->content) {
size_t len = strlen(cmd) + files[M_PRE_COMMAND]->size + 2;
rc = cext_emalloc(len);
snprintf(rc, len, "%s %s", (char *) files[M_PRE_COMMAND]->content,
cmd);
rc = cext_emallocz(len);
snprintf(rc, len, "%s %s", (char *) files[M_PRE_COMMAND]->content, cmd);
}
/* fallback */
spawn(dpy, rc);
@ -240,7 +239,7 @@ static int update_items(char *pattern)
items = 0;
item_size = size;
if (item_size)
items = (File **) cext_emalloc((item_size + 1) * sizeof(File *));
items = (File **) cext_emallocz((item_size + 1) * sizeof(File *));
}
size = 0;

View File

@ -3,7 +3,7 @@
include ../config.mk
SRC = container.c emalloc.c estrdup.c strlcat.c strlcpy.c strtonum.c tokenize.c
SRC = container.c emallocz.c estrdup.c strlcat.c strlcpy.c strtonum.c tokenize.c
OBJ = ${SRC:.c=.o}

View File

@ -52,8 +52,8 @@ int count_items(void **items);
int index_next_item(void **items, void *item);
int index_prev_item(void **items, void *item);
/* emalloc.c */
void *cext_emalloc(size_t size);
/* emallocz.c */
void *cext_emallocz(size_t size);
/* estrdup.c */
char *cext_estrdup(const char *s);

View File

@ -9,8 +9,6 @@
#include "cext.h"
static CItem zero_item = { 0 };
static int comp_ptr(void *p1, void *p2)
{
return p1 == p2;
@ -51,9 +49,8 @@ static void attach_to_stack(Container *c, CItem *i)
void cext_attach_item(Container *c, void *item)
{
CItem *i, *new = cext_emalloc(sizeof(CItem));
CItem *i, *new = cext_emallocz(sizeof(CItem));
fprintf(stderr, "XX %s\n", "cext_attach_item()");
*new = zero_item;
new->item = item;
if (!c->list)
c->list = new;
@ -204,7 +201,7 @@ void **attach_item_begin(void **old, void *item, size_t size_item)
int i, size_old;
void **result = 0;
for (size_old = 0; old && old[size_old]; size_old++);
result = cext_emalloc(size_item * (size_old + 2));
result = cext_emallocz(size_item * (size_old + 2));
result[0] = item;
for (i = 0; old && old[i]; i++)
result[i + 1] = old[i];
@ -219,7 +216,7 @@ void **attach_item_end(void **old, void *item, size_t size_item)
int i, size_old;
void **result = 0;
for (size_old = 0; old && old[size_old]; size_old++);
result = cext_emalloc(size_item * (size_old + 2));
result = cext_emallocz(size_item * (size_old + 2));
for (i = 0; old && old[i]; i++)
result[i] = old[i];
result[i++] = item;
@ -235,7 +232,7 @@ void **detach_item(void **old, void *item, size_t size_item)
void **result = 0;
for (size_old = 0; old && old[size_old]; size_old++);
if (size_old != 1) {
result = cext_emalloc(size_item * size_old);
result = cext_emallocz(size_item * size_old);
for (i = 0; old[i]; i++)
if (old[i] != item)
result[j++] = old[i];

View File

@ -8,9 +8,9 @@
#include "cext.h"
void *cext_emalloc(size_t size)
void *cext_emallocz(size_t size)
{
void *res = malloc(size);
void *res = calloc(1, size);
if (!res) {
fprintf(stderr, "fatal: could not malloc() %d bytes\n",

View File

@ -13,7 +13,7 @@ char *cext_estrdup(const char *s)
{
char *tmp;
tmp = (char *) cext_emalloc(strlen(s) + 1);
tmp = (char *) cext_emallocz(strlen(s) + 1);
strcpy(tmp, (char *) s);
return tmp;

View File

@ -16,7 +16,6 @@
#include <cext.h>
static IXPClient zero_client = { 0 };
static size_t offsets[MAX_CONN * MAX_OPEN_FILES][2]; /* set of possible fd's */
static void check_error(IXPClient * c, void *msg)
@ -117,7 +116,7 @@ static void *poll_server(IXPClient * c, void *request, size_t req_len,
handle_dead_server(c);
return 0;
}
result = cext_emalloc(*out_len);
result = cext_emallocz(*out_len);
header = 1;
}
r = read(c->fd, ((char *) result) + num, *out_len - num);
@ -256,8 +255,7 @@ IXPClient *init_ixp_client(char *sockfile)
socklen_t su_len;
/* init */
IXPClient *c = (IXPClient *) cext_emalloc(sizeof(IXPClient));
*c = zero_client;
IXPClient *c = (IXPClient *) cext_emallocz(sizeof(IXPClient));
c->create = cixp_create;
c->open = cixp_open;
c->read = cixp_read;

View File

@ -10,14 +10,12 @@
#include <cext.h>
/* request messages ------------------------------------------------ */
void *tcreate_message(char *path, size_t * msg_len)
{
char *msg;
ReqHeader h;
*msg_len = sizeof(ReqHeader) + strlen(path) + 1;
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.req = TCREATE;
memcpy(msg, &h, sizeof(ReqHeader));
memcpy(msg + sizeof(ReqHeader), path, strlen(path) + 1);
@ -29,7 +27,7 @@ void *topen_message(char *path, size_t * msg_len)
char *msg;
ReqHeader h;
*msg_len = sizeof(ReqHeader) + strlen(path) + 1;
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.req = TOPEN;
memcpy(msg, &h, sizeof(ReqHeader));
memcpy(msg + sizeof(ReqHeader), path, strlen(path) + 1);
@ -42,7 +40,7 @@ void *tread_message(int fd, size_t offset, size_t buf_len,
char *msg;
ReqHeader h;
*msg_len = sizeof(ReqHeader);
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.req = TREAD;
h.fd = fd;
h.offset = offset;
@ -57,7 +55,7 @@ void *twrite_message(int fd, size_t offset, void *content,
char *msg;
ReqHeader h;
*msg_len = sizeof(ReqHeader) + content_len;
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.req = TWRITE;
h.fd = fd;
h.offset = offset;
@ -72,7 +70,7 @@ void *tclose_message(int fd, size_t * msg_len)
char *msg;
ReqHeader h;
*msg_len = sizeof(ReqHeader);
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.req = TCLUNK;
h.fd = fd;
memcpy(msg, &h, sizeof(ReqHeader));
@ -84,21 +82,19 @@ void *tremove_message(char *path, size_t * msg_len)
char *msg;
ReqHeader h;
*msg_len = sizeof(ReqHeader) + strlen(path) + 1;
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.req = TREMOVE;
memcpy(msg, &h, sizeof(ReqHeader));
memcpy(msg + sizeof(ReqHeader), path, strlen(path) + 1);
return msg;
}
/* response messages ----------------------------------------------- */
void *rcreate_message(size_t * msg_len)
{
char *msg;
ResHeader h;
*msg_len = sizeof(ResHeader);
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.res = RCREATE;
memcpy(msg, &h, sizeof(ResHeader));
return msg;
@ -109,7 +105,7 @@ void *ropen_message(int fd, size_t * msg_len)
char *msg;
ResHeader h;
*msg_len = sizeof(ResHeader);
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.res = ROPEN;
h.fd = fd;
memcpy(msg, &h, sizeof(ResHeader));
@ -121,7 +117,7 @@ void *rread_message(void *content, size_t content_len, size_t * msg_len)
char *msg;
ResHeader h;
*msg_len = sizeof(ResHeader) + content_len;
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.res = RREAD;
h.buf_len = content_len;
memcpy(msg, &h, sizeof(ResHeader));
@ -134,7 +130,7 @@ void *rwrite_message(size_t * msg_len)
char *msg;
ResHeader h;
*msg_len = sizeof(ResHeader);
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.res = RWRITE;
memcpy(msg, &h, sizeof(ResHeader));
return msg;
@ -145,7 +141,7 @@ void *rclose_message(size_t * msg_len)
char *msg;
ResHeader h;
*msg_len = sizeof(ResHeader);
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.res = RCLUNK;
memcpy(msg, &h, sizeof(ResHeader));
return msg;
@ -156,7 +152,7 @@ void *rremove_message(size_t * msg_len)
char *msg;
ResHeader h;
*msg_len = sizeof(ResHeader);
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.res = RREMOVE;
memcpy(msg, &h, sizeof(ResHeader));
return msg;
@ -168,7 +164,7 @@ void *rerror_message(char *errstr, size_t * msg_len)
size_t len = strlen(errstr) + 1;
ResHeader h;
*msg_len = sizeof(ResHeader) + len;
msg = cext_emalloc(*msg_len);
msg = cext_emallocz(*msg_len);
h.res = RERROR;
memcpy(msg, &h, sizeof(ResHeader));
memmove(msg + sizeof(ResHeader), errstr, len);

View File

@ -11,8 +11,6 @@
#include <cext.h>
static File zero_file = { 0 };
int is_directory(File * f)
{
return !f->size && f->content;
@ -53,8 +51,7 @@ File *ixp_create(IXPServer * s, char *path)
}
/* only create missing parts, if file is directory */
while (tok) {
f = (File *) cext_emalloc(sizeof(File));
*f = zero_file;
f = (File *) cext_emallocz(sizeof(File));
f->name = strdup(tok);
f->parent = p;
if (p->content) {
@ -90,7 +87,7 @@ static char *_ls(File * f)
for (p = f; p; p = p->next)
num++;
tmp = cext_emalloc(sizeof(File *) * num);
tmp = cext_emallocz(sizeof(File *) * num);
i = 0;
for (p = f; p; p = p->next) {
size += strlen(p->name) + 1;
@ -99,7 +96,7 @@ static char *_ls(File * f)
tmp[i++] = p;
}
qsort(tmp, num, sizeof(char *), comp_file_name);
result = cext_emalloc(size);
result = cext_emallocz(size);
result[0] = '\0';
for (i = 0; i < num; i++) {
strncat(result, tmp[i]->name, size);

View File

@ -17,8 +17,6 @@
#include <cext.h>
static File zero_file = { 0 };
static IXPServer zero_server = { 0 };
static Connection zero_conn = { 0 };
static int user_fd = -1;
@ -81,7 +79,7 @@ static void handle_ixp_read(Connection * c, ReqHeader * h)
void *data = 0;
size_t out_len;
data = cext_emalloc(h->buf_len);
data = cext_emallocz(h->buf_len);
out_len = c->s->read(c->s, h->fd, h->offset, data, h->buf_len);
free(c->data);
if (c->s->errstr) {
@ -219,7 +217,7 @@ static void read_conn(Connection * c)
return;
}
c->remain = c->len;
c->data = cext_emalloc(c->len);
c->data = cext_emallocz(c->len);
c->header = 1;
}
r = read(c->fd, ((char *) c->data) + c->len - c->remain, c->remain);
@ -347,10 +345,9 @@ IXPServer *init_server(char *sockfile, void (*cleanup) (void))
IXPServer *s;
/* init */
s = (IXPServer *) cext_emalloc(sizeof(IXPServer));
*s = zero_server;
s = (IXPServer *) cext_emallocz(sizeof(IXPServer));
s->sockfile = sockfile;
s->root = (File *) cext_emalloc(sizeof(File));
s->root = (File *) cext_emallocz(sizeof(File));
s->runlevel = HALT; /* initially server is not running */
s->create = ixp_create;
s->remove = ixp_remove;
@ -358,7 +355,6 @@ IXPServer *init_server(char *sockfile, void (*cleanup) (void))
s->close = ixp_close;
s->read = ixp_read;
s->write = ixp_write;
*s->root = zero_file;
s->root->name = strdup("");
for (i = 0; i < MAX_CONN; i++) {
s->conn[i].s = s;

View File

@ -21,7 +21,7 @@ int main(int argc, char *argv[])
printf("--------------------------------\n");
{
int *e = cext_emalloc(sizeof(int));
int *e = cext_emallocz(sizeof(int));
cext_attach_item(&c, e);
cext_iterate(&c, nil, iter_print_container);
cext_detach_item(&c, e);
@ -31,7 +31,7 @@ int main(int argc, char *argv[])
printf("--------------------------------\n");
for (i = 0; i < 10; i++) {
int *e = cext_emalloc(sizeof(int));
int *e = cext_emallocz(sizeof(int));
*e = i;
cext_attach_item(&c, e);
}