int zallocations() { CLOSED * p1 = (CLOSED*)myzalloc(5, sizeof(CLOSED)); CLOSED * __INDEX p2 = (CLOSED*)myzalloc(7, sizeof(CLOSED)); OPEN *p3 = (OPEN*)myzalloc(1, 64 + sizeof(OPEN)); THEWILD * __WILD pw = (THEWILD *)myzalloc(36, 1); p1 = (CLOSED*) myzalloc(1, sizeof(CLOSED)); return 5; }
static int acc_conn_create(struct teamd_context *ctx, int sock) { struct usock_acc_conn *acc_conn; int err; acc_conn = myzalloc(sizeof(*acc_conn)); if (!acc_conn) { teamd_log_err("usock: No memory to allocate new connection structure."); return -ENOMEM; } acc_conn->sock = sock; err = teamd_loop_callback_fd_add(ctx, USOCK_ACC_CONN_CB_NAME, acc_conn, callback_usock_acc_conn, acc_conn->sock, TEAMD_LOOP_FD_EVENT_READ); if (err) goto free_acc_conn; teamd_loop_callback_enable(ctx, USOCK_ACC_CONN_CB_NAME, acc_conn); list_add(&ctx->usock.acc_conn_list, &acc_conn->list); return 0; free_acc_conn: free(acc_conn); return err; }
void command_add (command_fn_t callback, const char *input, const char *readable) { if (!commands) { commands = tree_alloc(TREE_KEY_STRING, "TREE ROOT: commands"); } command_t *command = (typeof(command)) myzalloc(sizeof(*command), "TREE NODE: command"); command->tree.key = dupstr(input, "TREE KEY: command"); command->callback = callback; /* * Convert the command into tokens for matching. */ tokens_tostring(input, &command->tokens); tokens_tostring(input, &command->input_tokens); tokens_tostring(readable, &command->readable_tokens); if (!tree_insert(commands, &command->tree.node)) { ERR("insert of command %s fail", input); } }
static int do_func(char **fields, int nfields, char **err) { char *name; char *rtype; char *atype; char *hdrs; char *envs; struct sym_test *st; if (nfields != 5) { myasprintf(err, "number of fields (%d) != 5", nfields); return (-1); } name = fields[0]; rtype = fields[1]; atype = fields[2]; hdrs = fields[3]; envs = fields[4]; st = myzalloc(sizeof (*st)); st->st_type = SYM_FUNC; st->st_name = mystrdup(name); st->st_rtype = mystrdup(rtype); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0) || (add_arg_types(st, atype, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
static int do_value(char **fields, int nfields, char **err) { char *name; char *type; char *hdrs; char *envs; struct sym_test *st; if (nfields != 4) { myasprintf(err, "number of fields (%d) != 4", nfields); return (-1); } name = fields[0]; type = fields[1]; hdrs = fields[2]; envs = fields[3]; st = myzalloc(sizeof (*st)); st->st_type = SYM_VALUE; st->st_name = mystrdup(name); st->st_rtype = mystrdup(type); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
static int do_type(char **fields, int nfields, char **err) { char *decl; char *hdrs; char *envs; struct sym_test *st; if (nfields != 3) { myasprintf(err, "number of fields (%d) != 3", nfields); return (-1); } decl = fields[0]; hdrs = fields[1]; envs = fields[2]; st = myzalloc(sizeof (*st)); st->st_type = SYM_TYPE; st->st_name = mystrdup(decl); st->st_rtype = mystrdup(decl); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
static int do_env_group(char **fields, int nfields, char **err) { char *name; char *list; struct env_group *eg; uint64_t mask; char *item; if (nfields != 2) { myasprintf(err, "number of fields (%d) != 2", nfields); return (-1); } name = fields[0]; list = fields[1]; mask = 0; if (expand_env(list, &mask, &item) < 0) { myasprintf(err, "reference to undefined env %s", item); return (-1); } eg = myzalloc(sizeof (*eg)); eg->eg_name = mystrdup(name); eg->eg_mask = mask; eg->eg_next = env_groups; env_groups = eg; return (0); }
/* * Load a new font and create textures for each glyph */ font *ttf_new (const char *name, int32_t pointSize, int32_t style) { TTF_Font *ttf; uint8_t c; font *f; f = (fontp)myzalloc(sizeof(*f), __FUNCTION__); DBG("Load TTF: %s", name); ttf = TTF_OpenFont(name, pointSize); if (!ttf) { DIE("cannot open font file %s", name); } f->foreground.r = 255; f->foreground.g = 255; f->foreground.b = 255; f->background.r = 0; f->background.g = 0; f->background.b = 0; TTF_SetFontStyle(ttf, style); for (c = TTF_GLYPH_MIN; c < TTF_GLYPH_MAX; c++) { ttf_create_tex_from_char(ttf, name, f, c); } TTF_CloseFont(ttf); return (f); }
static struct ndp_msg *ndp_msg_alloc(void) { struct ndp_msg *msg; msg = myzalloc(sizeof(*msg)); if (!msg) return NULL; msg->icmp6_hdr = (struct icmp6_hdr *) msg->buf; return msg; }
musicp music_load (const char *file, const char *name_alias) { if (name_alias) { musicp m = music_find(name_alias); if (m) { return (m); } } if (!file) { if (!name_alias) { ERR("no file for music"); } else { ERR("no file for music loading %s", name_alias); } } if (!all_music) { all_music = tree_alloc(TREE_KEY_STRING, "TREE ROOT: music"); } musicp m = (typeof(m)) myzalloc(sizeof(*m), "TREE NODE: music"); m->tree.key = dupstr(name_alias, "TREE KEY: music"); if (!tree_insert(all_music, &m->tree.node)) { ERR("music insert name_alias [%s] failed", name_alias); } m->data = ramdisk_load(file, &m->len); if (!m->data) { ERR("cannot load music %s", file); } SDL_RWops *rw; rw = SDL_RWFromMem(m->data, m->len); if (!rw) { ERR("cannot make RW music %s", file); } #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2 /* { */ m->music = Mix_LoadMUS_RW(rw); #else m->music = Mix_LoadMUS_RW(rw, false); #endif /* } */ if (!m->music) { MSG_BOX("Mix_LoadMUS_RW fail %s: %s %s", file, Mix_GetError(), SDL_GetError()); SDL_ClearError(); } DBG("Load %s", file); return (m); }
uint8_t ptrcheck_test (int32_t argc, char *argv[]) { void *p; ptrcheck_leak_snapshot(); p = myzalloc(10, "ptrcheck_test a"); verify(p); myfree(p); p = mymalloc(10, "ptrcheck_test b"); verify(p); return (0); }
/** * teamdctl_alloc: * * Allocates library context and does initial setup. * * Returns: new libteam library context **/ TEAMDCTL_EXPORT struct teamdctl *teamdctl_alloc(void) { struct teamdctl *tdc; const char *env; tdc = myzalloc(sizeof(*tdc)); if (!tdc) return NULL; tdc->log_fn = log_stderr; tdc->log_priority = LOG_ERR; /* environment overwrites config */ env = getenv("TEAMDCTL_LOG"); if (env != NULL) teamdctl_set_log_priority(tdc, log_priority(env)); dbg(tdc, "teamdctl %p created.", tdc); dbg(tdc, "log_priority=%d", tdc->log_priority); return tdc; }
static int cli_init(struct teamdctl *tdc, const char *team_name) { int err; if (tdc->cli->priv_size) { tdc->cli_priv = myzalloc(tdc->cli->priv_size); if (!tdc->cli_priv) return -ENOMEM; } err = tdc->cli->init(tdc, team_name, tdc->cli_priv); if (err) goto free_priv; if (tdc->cli->test_method_call_required) { err = cli_method_call(tdc, "ConfigDump", NULL, ""); if (err) goto free_priv; } return 0; free_priv: if (tdc->cli->priv_size) free(tdc->cli_priv); return err; }
static int do_define(char **fields, int nfields, char **err) { char *name, *value, *hdrs, *envs; struct sym_test *st; if (nfields != 4) { myasprintf(err, "number of fields (%d) != 4", nfields); return (-1); } name = fields[0]; value = fields[1]; hdrs = fields[2]; envs = fields[3]; st = myzalloc(sizeof (*st)); st->st_type = SYM_DEFINE; st->st_name = mystrdup(name); /* * A value to compare against is optional. trim will leave it as a null * pointer if there's nothing there. */ test_trim(&value); if (*value != '\0') st->st_defval = mystrdup(value); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
tree_demarshal *demarshal (const char *filename) { demarshal_parse_state state; int8_t depth; #ifdef FASTER_LOADING static tree_demarshal *ctx; #else tree_demarshal *ctx; #endif uint8_t newline; char *next; char *buf; int32_t size; char *at; float tmp_float; char *tmp_at = 0; int64_t tmp_int = 0; uint64_t tmp_hex = 0; int64_t tmp_mul = 0; uint8_t compress; strncpy(demarshal_parse_filename, filename, sizeof(demarshal_parse_filename) - 1); demarshal_parse_line = 1; if (!filename) { ERR("no filename"); } buf = (char*)ramdisk_load(filename, &size); if (!buf) { return (0); } #ifdef FASTER_LOADING if (!ctx) { ctx = (typeof(ctx)) myzalloc(sizeof(*ctx), "demarshal ctx"); } #else ctx = (typeof(ctx)) myzalloc(sizeof(*ctx), "demarshal ctx"); #endif demarshal_buf_end = buf + size; next = buf; at = buf; depth = 0; state = MARSHAL_PARSE_STATE_NONE; newline = 1; while (at < demarshal_buf_end) { at = next; if (newline) { while (at < demarshal_buf_end) { if (*at == '#') { while ((at < demarshal_buf_end) && (*at != '\n')) { at++; } at++; } else { while ((at < demarshal_buf_end) && isspace(*at)) { at++; } break; } } newline = 0; } if (at >= demarshal_buf_end) { break; } char c = *at; next = at + 1; if (at >= demarshal_buf_end) { ERR("Unexpected end in \"%s\"", filename); } switch (state) { case MARSHAL_PARSE_STATE_NONE: switch (c) { case ' ': case '\t': continue; case '#': while ((at < demarshal_buf_end) && (*at != '\n')) { at++; } next = at; continue; default: state = MARSHAL_PARSE_STATE_NAME; tmp_at = at; continue; case '0': if (*next == 'x') { next++; state = MARSHAL_PARSE_STATE_HEX; tmp_at = at; continue; } case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': state = MARSHAL_PARSE_STATE_INT; tmp_int = c - '0'; tmp_mul = 1; tmp_at = at; continue; case '\"': state = MARSHAL_PARSE_STATE_STRING; tmp_at = at + 1; continue; case '[': case '{': demarshal_push_node(ctx, depth, MARSHAL_BRA); depth++; continue; case ']': case '}': depth--; demarshal_push_node(ctx, depth, MARSHAL_KET); continue; case 13: demarshal_parse_line++; newline = 1; continue; case '\n': demarshal_parse_line++; newline = 1; continue; case '-': state = MARSHAL_PARSE_STATE_INT; tmp_int = 0; tmp_at = at; tmp_mul = -1; continue; case '.': case '+': state = MARSHAL_PARSE_STATE_INT; tmp_int = 0; tmp_at = at; tmp_mul = 1; continue; } case MARSHAL_PARSE_STATE_INT: while (isdigit(c)) { tmp_int *= 10; tmp_int += c - '0'; c = *++at; if (at >= demarshal_buf_end) { ERR("Unexpected end in \"%s\" when parsing int", filename); } } if (c == '.') { uint8_t need_lex = 0; tmp_float = (float)tmp_int; float mul = 10.0f; c = *++at; while (!isspace(c)) { if (at >= demarshal_buf_end) { ERR("Unexpected end in \"%s\" when parsing float", filename); } if (need_lex) { c = *++at; continue; } if (c == 'e') { c = *++at; while (!isspace(c)) { c = *++at; } break; } tmp_float += (float)(c - '0') / mul; mul *= 10.0f; c = *++at; } if (need_lex) { *at++ = '\0'; tmp_float = strtof(tmp_at, 0); } else { tmp_float *= (float)tmp_mul; } demarshal_push_float(ctx, depth, tmp_float); } else { tmp_int *= tmp_mul; demarshal_push_int64(ctx, depth, tmp_int); } state = MARSHAL_PARSE_STATE_NONE; next = at; continue; case MARSHAL_PARSE_STATE_HEX: tmp_hex = 0; c = *at; while (isdigit(c) || ((c >= 'a') && (c <='f')) || ((c >= 'A') && (c <='F'))) { tmp_hex *= 16; if ((c >= 'a') && (c <='z')) { tmp_hex += c - 'a' + 10; } else if ((c >= 'A') && (c <='Z')) { tmp_hex += c - 'A' + 10; } else { tmp_hex += c - '0'; } c = *++at; if (at >= demarshal_buf_end) { ERR("Unexpected end in \"%s\" when parsing int", filename); } } demarshal_push_int64(ctx, depth, (int64_t)tmp_hex); state = MARSHAL_PARSE_STATE_NONE; next = at; continue; case MARSHAL_PARSE_STATE_NAME: while ((c != '=') && (c != ' ') && (c != '\t') && (c != '\n')) { c = *++at; if (at >= demarshal_buf_end) { ERR("Unexpected end in \"%s\" when parsing name", filename); } } if (c == '\n') { newline = 1; } state = MARSHAL_PARSE_STATE_NONE; *at = '\0'; next = at + 1; demarshal_push_name(ctx, depth, tmp_at); continue; case MARSHAL_PARSE_STATE_STRING: compress = 0; while (c != '\"') { c = *++at; if (c == '\\') { at++; compress = 1; } if (at >= demarshal_buf_end) { ERR("Unexpected end in \"%s\" when parsing string", filename); } } state = MARSHAL_PARSE_STATE_NONE; *at = '\0'; next = at + 1; if (compress) { strnoescape(tmp_at); } demarshal_push_string(ctx, depth, tmp_at); continue; } } if (state != MARSHAL_PARSE_STATE_NONE) { ERR("Error in \"%s\" line %u", demarshal_parse_filename, demarshal_parse_line); } ctx->nodeat = 0; myfree(buf); #ifdef ENABLE_DEMARSHAL_DEBUG demarshal_print(ctx); #endif return (ctx); }