Changeset: 49ca45c09fc7 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=49ca45c09fc7
Modified Files:
        clients/mapilib/mapi.c
        common/stream/stream.c
Branch: default
Log Message:

Use compound literals to initialized just allocated memory.
This has the advantage over the old way in that it is shorter (no need
to mention the zeros and nulls), and there is no danger that we forget
fields (e.g. ones that get added later).


diffs (truncated from 368 to 300 lines):

diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -1409,7 +1409,15 @@ new_result(MapiHdl hdl)
        result = malloc(sizeof(*result));
        if (result == NULL)
                return NULL;
-       result->next = NULL;
+       *result = (struct MapiResultSet) {
+               .hdl = hdl,
+               .tableid = -1,
+               .querytype = -1,
+               .last_id = -1,
+               .cache.rowlimit = hdl->mid->cachelimit,
+               .cache.reader = -1,
+               .commentonly = true,
+       };
        if (hdl->lastresult == NULL)
                hdl->result = hdl->lastresult = result;
        else {
@@ -1417,33 +1425,6 @@ new_result(MapiHdl hdl)
                hdl->lastresult = result;
        }
 
-       result->hdl = hdl;
-       result->tableid = -1;
-       result->querytype = -1;
-       result->errorstr = NULL;
-       result->querytime = 0;
-       result->maloptimizertime = 0;
-       result->sqloptimizertime = 0;
-       memset(result->sqlstate, 0, sizeof(result->sqlstate));
-
-       result->tuple_count = 0;
-       result->row_count = 0;
-       result->last_id = -1;
-
-       result->fieldcnt = 0;
-       result->maxfields = 0;
-       result->fields = NULL;
-
-       result->cache.rowlimit = hdl->mid->cachelimit;
-       result->cache.limit = 0;
-       result->cache.writer = 0;
-       result->cache.reader = -1;
-       result->cache.first = 0;
-       result->cache.tuplecount = 0;
-       result->cache.line = NULL;
-
-       result->commentonly = true;
-
        return result;
 }
 
@@ -1693,21 +1674,11 @@ mapi_new_handle(Mapi mid)
                mapi_setError(mid, "Memory allocation failure", 
"mapi_new_handle", MERROR);
                return NULL;
        }
-       hdl->mid = mid;
-       hdl->template = NULL;
-       hdl->query = NULL;
-       hdl->maxbindings = 0;
-       hdl->bindings = NULL;
-       hdl->maxparams = 0;
-       hdl->params = NULL;
-       hdl->result = NULL;
-       hdl->lastresult = NULL;
-       hdl->active = NULL;
-       hdl->needmore = false;
-       hdl->pending_close = NULL;
-       hdl->npending_close = 0;
+       *hdl = (struct MapiStatement) {
+               .mid = mid,
+               .needmore = false,
+       };
        /* add to doubly-linked list */
-       hdl->prev = NULL;
        hdl->next = mid->first;
        mid->first = hdl;
        if (hdl->next)
diff --git a/common/stream/stream.c b/common/stream/stream.c
--- a/common/stream/stream.c
+++ b/common/stream/stream.c
@@ -692,31 +692,20 @@ create_stream(const char *name)
                return NULL;
        if ((s = (stream *) malloc(sizeof(*s))) == NULL)
                return NULL;
-       s->swapbytes = false;
-       s->readonly = true;
-       s->isutf8 = false;      /* not known for sure */
-       s->binary = false;
-       s->name = strdup(name);
+       *s = (stream) {
+               .swapbytes = false,
+               .readonly = true,
+               .isutf8 = false,        /* not known for sure */
+               .binary = false,
+               .name = strdup(name),
+               .errnr = MNSTR_NO__ERROR,
+               .error = error,
+               .destroy = destroy,
+       };
        if(s->name == NULL) {
                free(s);
                return NULL;
        }
-       s->stream_data.p = NULL;
-       s->errnr = MNSTR_NO__ERROR;
-       s->read = NULL;
-       s->write = NULL;
-       s->close = NULL;
-       s->clrerr = NULL;
-       s->error = error;
-       s->destroy = destroy;
-       s->flush = NULL;
-       s->fsync = NULL;
-       s->fgetpos = NULL;
-       s->fsetpos = NULL;
-       s->timeout = 0;
-       s->timeout_func = NULL;
-       s->update_timeout = NULL;
-       s->isalive = NULL;
 #ifdef STREAM_DEBUG
        fprintf(stderr, "create_stream %s -> %p\n",
                name ? name : "<unnamed>", s);
@@ -1264,6 +1253,7 @@ open_bzstream(const char *restrict filen
                free(bzp);
                return NULL;
        }
+       *bzp = (struct bz) {0};
        fl[0] = flags[0];       /* 'r' or 'w' */
        fl[1] = 'b';            /* always binary */
        fl[2] = '\0';
@@ -1893,12 +1883,15 @@ open_lz4stream(const char *restrict file
 
        if ((lz4 = malloc(sizeof(struct lz4_stream))) == NULL)
                return NULL;
-       if ((lz4->ring_buffer = malloc(buffer_size)) == NULL) {
+       *lz4 = (struct lz4_stream) {
+               .ring_buffer = malloc(buffer_size),
+               .total_processing = (flags[0] == 'r') ? buffer_size : 0,
+               .ring_buffer_size = buffer_size,
+       };
+       if (lz4->ring_buffer == NULL) {
                free(lz4);
                return NULL;
        }
-       lz4->total_processing = (flags[0] == 'r') ? buffer_size : 0;
-       lz4->ring_buffer_size = buffer_size;
 
        if(flags[0] == 'w') {
                error_code = 
LZ4F_createCompressionContext(&(lz4->context.comp_context), LZ4F_VERSION);
@@ -2326,10 +2319,9 @@ open_urlstream(const char *url)
 
        if ((c = malloc(sizeof(*c))) == NULL)
                return NULL;
-       c->handle = NULL;
-       c->buffer = NULL;
-       c->maxsize = c->usesize = c->offset = 0;
-       c->running = 1;
+       *c = (struct curl_data) {
+               .running = 1,
+       };
        if ((s = create_stream(url)) == NULL) {
                free(c);
                return NULL;
@@ -3027,15 +3019,14 @@ file_rastream(FILE *restrict fp, const c
 #ifdef _MSC_VER
        if (fileno(fp) == 0 && isatty(0)) {
                struct console *c = malloc(sizeof(struct console));
-               if(c == NULL) {
+               if (c == NULL) {
                        destroy(s);
                        return NULL;
                }
                s->stream_data.p = c;
-               c->h = GetStdHandle(STD_INPUT_HANDLE);
-               c->i = 0;
-               c->len = 0;
-               c->rd = 0;
+               *c = (struct console) {
+                       .h = GetStdHandle(STD_INPUT_HANDLE),
+               };
                s->read = console_read;
                s->write = NULL;
                s->destroy = console_destroy;
@@ -3068,15 +3059,14 @@ file_wastream(FILE *restrict fp, const c
 #ifdef _MSC_VER
        if ((fileno(fp) == 1 || fileno(fp) == 2) && isatty(fileno(fp))) {
                struct console *c = malloc(sizeof(struct console));
-               if(c == NULL) {
+               if (c == NULL) {
                        destroy(s);
                        return NULL;
                }
                s->stream_data.p = c;
-               c->h = GetStdHandle(STD_OUTPUT_HANDLE);
-               c->i = 0;
-               c->len = 0;
-               c->rd = 0;
+               *c = (struct console) {
+                       .h = GetStdHandle(STD_OUTPUT_HANDLE),
+               };
                s->read = NULL;
                s->write = console_write;
                s->destroy = console_destroy;
@@ -3414,10 +3404,12 @@ ic_open(iconv_t cd, stream *restrict ss,
                return NULL;
        }
        s->stream_data.p = ic;
-       ic->cd = cd;
-       ic->s = ss;
-       ic->buflen = 0;
-       ic->eof = false;
+       *ic = (struct icstream) {
+               .cd = cd,
+               .s = ss,
+               .buflen = 0,
+               .eof = false,
+       };
        return s;
 }
 
@@ -3521,13 +3513,14 @@ buffer_create(size_t size)
 
        if ((b = malloc(sizeof(*b))) == NULL)
                return NULL;
-       b->pos = 0;
-       b->buf = malloc(size);
+       *b = (buffer) {
+               .buf = malloc(size),
+               .len = size,
+       };
        if (b->buf == NULL) {
                free(b);
                return NULL;
        }
-       b->len = size;
        return b;
 }
 
@@ -3707,11 +3700,9 @@ bs_create(stream *s)
 
        if ((ns = malloc(sizeof(*ns))) == NULL)
                return NULL;
-       ns->s = s;
-       ns->nr = 0;
-       ns->itotal = 0;
-       ns->blks = 0;
-       ns->bytes = 0;
+       *ns = (bs) {
+               .s = s,
+       };
        return ns;
 }
 
@@ -4185,18 +4176,17 @@ bs2_create(stream *s, size_t bufsiz, com
 
        if ((ns = malloc(sizeof(*ns))) == NULL)
                return NULL;
-       if ((ns->buf = malloc(bufsiz)) == NULL) {
+       *ns = (bs2) {
+               .buf = malloc(bufsiz),
+               .s = s,
+               .bufsiz = bufsiz,
+               .comp = comp,
+       };
+       if (ns->buf == NULL) {
                free(ns);
                return NULL;
        }
 
-       ns->s = s;
-       ns->nr = 0;
-       ns->itotal = 0;
-       ns->bufsiz = bufsiz;
-       ns->comp = comp;
-       ns->compbuf = NULL;
-
        compress_bound = compression_size_bound(ns);
        if (compress_bound > 0) {
                ns->compbufsiz = (size_t) compress_bound;
@@ -5141,19 +5131,19 @@ bstream_create(stream *s, size_t size)
                return NULL;
        if ((b = malloc(sizeof(*b))) == NULL)
                return NULL;
-       b->mode = size;
+       *b = (bstream) {
+               .mode = size,
+               .s = s,
+               .eof = false,
+       };
        if (size == 0)
                size = BUFSIZ;
-       b->s = s;
        b->buf = malloc(size + 1 + 1);
        if (b->buf == NULL) {
                free(b);
                return NULL;
        }
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to