static void add_define (const char * name, int nargs, const char * exps) { defn_t *p = lookup_definition(name); int h, len; /* trim off leading and trailing whitespace */ while (uisspace(*exps)) exps++; for (len = strlen(exps); len && uisspace(exps[len - 1]); len--); if (*exps == '#' && *(exps + 1) == '#') { yyerror("'##' at start of macro definition"); return; } if (len > 2 && *(exps + len - 2) == '#' && *(exps + len - 1) == '#') { yyerror("'##' at end of macro definition"); return; } if (p) { if (p->flags & DEF_IS_UNDEFINED) { p->exps = (char *)DREALLOC(p->exps, len + 1, TAG_COMPILER, "add_define: redef"); memcpy(p->exps, exps, len); p->exps[len] = 0; p->flags = 0; p->nargs = nargs; } else { if (p->flags & DEF_IS_PREDEF) { yyerror("Illegal to redefine predefined value."); return; } if (nargs != p->nargs || strcmp(exps, p->exps)) { char buf[200 + NSIZE]; sprintf(buf, "redefinition of #define %s\n", name); yywarn(buf); p->exps = (char *)DREALLOC(p->exps, len + 1, TAG_COMPILER, "add_define: redef"); memcpy(p->exps, exps, len); p->exps[len] = 0; p->nargs = nargs; } #ifndef LEXER p->flags &= ~DEF_IS_NOT_LOCAL; #endif } } else { p = ALLOCATE(defn_t, TAG_COMPILER, "add_define: def"); p->name = (char *) DXALLOC(strlen(name) + 1, TAG_COMPILER, "add_define: def name"); strcpy(p->name, name); p->exps = (char *) DXALLOC(len + 1, TAG_COMPILER, "add_define: def exps"); memcpy(p->exps, exps, len); p->exps[len] = 0; p->flags = 0; p->nargs = nargs; h = defhash(name); p->next = defns[h]; defns[h] = p; } }
static void add_define P3(char *, name, int, nargs, char *, exps) { defn_t *p = lookup_definition(name); int h; if (p) { if (p->flags & DEF_IS_UNDEFINED) { p->exps = (char *)DREALLOC(p->exps, strlen(exps) + 1, TAG_COMPILER, "add_define: redef"); strcpy(p->exps, exps); p->flags = 0; p->nargs = nargs; } else { if (p->flags & DEF_IS_PREDEF) { yyerror("Illegal to redefine predefined value."); return; } if (nargs != p->nargs || strcmp(exps, p->exps)) { char buf[200 + NSIZE]; sprintf(buf, "redefinition of #define %s\n", name); yywarn(buf); p->exps = (char *)DREALLOC(p->exps, strlen(exps) + 1, TAG_COMPILER, "add_define: redef"); strcpy(p->exps, exps); p->nargs = nargs; } #ifndef LEXER p->flags &= ~DEF_IS_NOT_LOCAL; #endif } } else { p = ALLOCATE(defn_t, TAG_COMPILER, "add_define: def"); p->name = (char *) DXALLOC(strlen(name) + 1, TAG_COMPILER, "add_define: def name"); strcpy(p->name, name); p->exps = (char *) DXALLOC(strlen(exps) + 1, TAG_COMPILER, "add_define: def exps"); strcpy(p->exps, exps); p->flags = 0; p->nargs = nargs; h = defhash(name); p->next = defns[h]; defns[h] = p; } }
char *extend_string (const char * str, int len) { malloc_block_t *mbt; #ifdef STRING_STATS int oldsize = MSTR_SIZE(str); #endif mbt = (malloc_block_t *)DREALLOC(MSTR_BLOCK(str), len + sizeof(malloc_block_t) + 1, TAG_MALLOC_STRING, "extend_string"); if (len < USHRT_MAX) { mbt->size = len; } else { mbt->size = USHRT_MAX; } ADD_STRING_SIZE(mbt->size - oldsize); CHECK_STRING_STATS; return (char *)(mbt + 1); }
int dterm_expand(dterm_t* p, size_t n) { ErlDrvTermData* new_base; size_t old_size = dterm_allocated_size(p); size_t new_size = old_size + n; size_t old_sz = old_size * sizeof(ErlDrvTermData); size_t new_sz = new_size * sizeof(ErlDrvTermData); ptrdiff_t offset = p->ptr - p->base; // offset of ptr if (p->base == p->data) { if ((new_base = DALLOC(new_sz)) == NULL) return 0; memcpy(new_base, p->base, old_sz); } else if ((new_base = DREALLOC(p->base, new_sz)) == NULL) return 0; p->base = new_base; p->ptr = p->base + offset; p->ptr_end = new_base + new_size; p->base = new_base; return 1; }