/* ** ydom_node_add_text() ** Create a text node and insert it as a child of an existing node. */ ydom_node_t *ydom_node_add_text(ydom_node_t *node, char *data) { ydom_node_t *text_node = NULL; char *tmp; if (!node) return (NULL); if (node->last_child && node->last_child->node_type == TEXT_NODE) { tmp = malloc0(strlen(node->last_child->value) + strlen(data) + 1); strcpy(tmp, node->last_child->value); strcat(tmp, data); free0(node->last_child->value); node->last_child->value = tmp; } else { if (!(text_node = malloc0(sizeof(ydom_node_t)))) return (NULL); text_node->node_type = TEXT_NODE; text_node->value = str2xmlentity(data); text_node->complete = YTRUE; _ydom_add_child_to_node(node, text_node); } return (text_node); }
/* ** ydom_new() ** Create a new XML DOM object. */ ydom_t *ydom_new() { ydom_t *dom; ydom_node_t *node; YLOG_MOD("ydom", YLOG_DEBUG, "Entering"); if (!(dom = malloc0(sizeof(ydom_t)))) { YLOG_ADD(YLOG_ERR, "Unable to allocate memory"); return (NULL); } if (!(node = malloc0(sizeof(ydom_node_t)))) { free0(dom); YLOG_ADD(YLOG_ERR, "Unable to allocate memory"); return (NULL); } node->node_type = DOCUMENT_NODE; node->complete = YTRUE; node->name = NULL; dom->error = YENOERR; dom->document_element = dom->current_parsed_node = node; YLOG_MOD("ydom", YLOG_DEBUG, "Exiting"); return (dom); }
DELAY create_delay (int run, int size, float* in, float* out, int rate, float tdelta, float tdelay) { DELAY a = (DELAY) malloc0 (sizeof (delay)); a->run = run; a->size = size; a->in = in; a->out = out; a->rate = rate; a->tdelta = tdelta; a->tdelay = tdelay; a->L = (int)(0.5 + 1.0 / (a->tdelta * (float)a->rate)); a->ft = 0.45 / (float)a->L; a->ncoef = (int)(60.0 / a->ft); a->ncoef = (a->ncoef / a->L + 1) * a->L; a->cpp = a->ncoef / a->L; a->phnum = (int)(0.5 + a->tdelay / a->tdelta); a->snum = a->phnum / a->L; a->phnum %= a->L; a->idx_in = 0; a->adelta = 1.0 / (a->rate * a->L); a->adelay = a->adelta * (a->snum * a->L + a->phnum); a->h = fir_bandpass (a->ncoef,-a->ft, +a->ft, 1.0, 1, 0, (float)a->L); a->rsize = a->cpp + (WSDEL - 1); a->ring = (float *) malloc0 (a->rsize * sizeof (complex)); InitializeCriticalSectionAndSpinCount ( &a->cs_update, 2500 ); return a; }
PORT ANB create_anb ( int run, int buffsize, float* in, float* out, float samplerate, float tau, float hangtime, float advtime, float backtau, float threshold ) { ANB a; a = (ANB) malloc0 (sizeof(anb)); a->run = run; a->buffsize = buffsize; a->in = in; a->out = out; a->samplerate = samplerate; a->tau = tau; a->hangtime = hangtime; a->advtime = advtime; a->backtau = backtau; a->threshold = threshold; a->wave = (float *) malloc0 (((int)(MAX_SAMPLERATE * MAX_TAU) + 1) * sizeof(float)); a->dline_size = (int)((MAX_TAU + MAX_ADVTIME) * MAX_SAMPLERATE) + 1; a->dline = (float *) malloc0 (a->dline_size * sizeof(complex)); InitializeCriticalSectionAndSpinCount (&a->cs_update, 2500); initBlanker(a); a->legacy = (float *) malloc0 (2048 * sizeof (complex)); /////////////// legacy interface - remove return a; }
/* ** _ydom_inside_hdlr() ** DOM handler for SAX parsing */ static void _ydom_inside_hdlr(ysax_t *sax, char *str) { ydom_t *dom; ydom_node_t *text; char *tmp; YLOG_MOD("ydom", YLOG_DEBUG, "Entering"); dom = (ydom_t*)YSAX_DATA(sax); if (dom->current_parsed_node->node_type == TEXT_NODE) { tmp = malloc0(strlen(dom->current_parsed_node->value) + strlen(str) + 1); strcpy(tmp, dom->current_parsed_node->value); strcat(tmp, str); free0(str); free0(dom->current_parsed_node->value); dom->current_parsed_node->value = tmp; } else { if (!(text = malloc0(sizeof(ydom_node_t)))) { YLOG_ADD(YLOG_ERR, "Unable to allocate memory"); return ; } text->node_type = TEXT_NODE; text->value = str; text->line_nbr = sax->line_nbr; _ydom_add_child_to_node(dom->current_parsed_node, text); dom->current_parsed_node = text; } YLOG_MOD("ydom", YLOG_DEBUG, "Exiting"); }
void shuffle1(const char *s, int l, int k) { int i, j, n_lets; s_ = s; l_ = l; k_ = k; if (k_ >= l_ || k_ <= 1) /* two special cases */ return; /* use hashtable to find distinct vertices */ n_lets = l_ - k_ + 2; /* number of (k-1)-lets */ n_vertices = 0; hinit(n_lets); for (i = 0; i < n_lets; i++) hinsert(i); root = entries[n_lets - 1].i_vertices; /* the last let */ if (vertices) free(vertices); vertices = malloc0(n_vertices * sizeof(vertex)); /* set i_sequence and n_indices for each vertex */ for (i = 0; i < n_lets; i++) { /* for each let */ hentry *ev = &entries[i]; vertex *v = &vertices[ev->i_vertices]; v->i_sequence = ev->i_sequence; if (i < n_lets - 1) /* not the last let */ v->n_indices++; } /* distribute indices for each vertex */ if (indices) free(indices); indices = malloc0((n_lets - 1) * sizeof(int)); j = 0; for (i = 0; i < n_vertices; i++) { /* for each vertex */ vertex *v = &vertices[i]; v->indices = indices + j; j += v->n_indices; } /* populate indices for each vertex */ for (i = 0; i < n_lets - 1; i++) { /* for each edge */ hentry *eu = &entries[i]; hentry *ev = &entries[i + 1]; vertex *u = &vertices[eu->i_vertices]; u->indices[u->i_indices++] = ev->i_vertices; } hcleanup(); }
float* fftcv_mults (int NM, float* c_impulse) { float* mults = (float *) malloc0 (NM * sizeof (complex)); float* cfft_impulse = (float *) malloc0 (NM * sizeof (complex)); fftwf_plan ptmp = fftwf_plan_dft_1d(NM, (fftwf_complex *) cfft_impulse, (fftwf_complex *) mults, FFTW_FORWARD, FFTW_PATIENT); memset (cfft_impulse, 0, NM * sizeof (complex)); // store complex coefs right-justified in the buffer memcpy (&(cfft_impulse[NM - 2]), c_impulse, (NM / 2 + 1) * sizeof(complex)); fftwf_execute (ptmp); fftwf_destroy_plan (ptmp); _aligned_free (cfft_impulse); return mults; }
GFont simply_res_add_custom_font(SimplyRes *self, uint32_t id) { SimplyFont *font = malloc0(sizeof(*font)); if (!font) { return NULL; } ResHandle handle = resource_get_handle(id); if (!handle) { return NULL; } GFont custom_font = fonts_load_custom_font(handle); if (!custom_font) { free(font); return NULL; } font->id = id; font->font = custom_font; list1_prepend(&self->fonts, &font->node); window_stack_schedule_top_window_render(); return font->font; }
void* malloc(size_t size) { void* ptr = malloc0(size); char buf[MAX_BUFFER]; int pos = 0; buf[pos++] = 'm'; buf[pos++] = ' '; pos += hexdump(&buf[pos], (char*) &ptr, sizeof(ptr)); buf[pos++] = ' '; pos += hexdump(&buf[pos], (char*) &size, sizeof(size)); pos += dump_callstack(&buf[pos]); #if 0 struct layout_t* lo = __builtin_frame_address(0); while (lo) { void* caller = lo->ret; buf[pos++] = ' '; pos += hexdump(&buf[pos], (char*) &caller, sizeof(caller)); lo = (struct layout_t*) lo->next; } #endif buf[pos++] = '\n'; pthread_mutex_lock(&mutex); write(fd, buf, pos); pthread_mutex_unlock(&mutex); return ptr; }
int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) { _cleanup_netlink_message_unref_ sd_netlink_message *m = NULL; const NLType *nl_type; size_t size; int r; r = type_system_get_type(&type_system_root, &nl_type, type); if (r < 0) return r; if (type_get_type(nl_type) != NETLINK_TYPE_NESTED) return -EINVAL; r = message_new_empty(rtnl, &m); if (r < 0) return r; size = NLMSG_SPACE(type_get_size(nl_type)); assert(size >= sizeof(struct nlmsghdr)); m->hdr = malloc0(size); if (!m->hdr) return -ENOMEM; m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; type_get_type_system(nl_type, &m->containers[0].type_system); m->hdr->nlmsg_len = size; m->hdr->nlmsg_type = type; *ret = m; m = NULL; return 0; }
static struct gatt_db_attribute *new_attribute(struct gatt_db_service *service, uint16_t handle, const bt_uuid_t *type, const uint8_t *val, uint16_t len) { struct gatt_db_attribute *attribute; attribute = new0(struct gatt_db_attribute, 1); attribute->service = service; attribute->handle = handle; attribute->uuid = *type; attribute->value_len = len; if (len) { attribute->value = malloc0(len); if (!attribute->value) goto failed; memcpy(attribute->value, val, len); } attribute->pending_reads = queue_new(); attribute->pending_writes = queue_new(); return attribute; failed: attribute_destroy(attribute); return NULL; }
static void handle_uhid_output(struct uhid_event *event, void *user_data) { struct uhid_output_req *output = &event->u.output; struct hid_device *dev = user_data; int fd, req_size; uint8_t *req; if (!dev->ctrl_io) return; req_size = 1 + output->size; req = malloc0(req_size); if (!req) return; req[0] = HID_MSG_SET_REPORT | output->rtype; memcpy(req + 1, output->data, req_size - 1); fd = g_io_channel_unix_get_fd(dev->ctrl_io); if (write(fd, req, req_size) < 0) error("hidhost: error writing set_report: %s (%d)", strerror(errno), errno); free(req); }
static DHCPMessage *create_message(uint8_t *options, uint16_t optlen, uint8_t *file, uint8_t filelen, uint8_t *sname, uint8_t snamelen) { DHCPMessage *message; size_t len = sizeof(DHCPMessage) + 4 + optlen; uint8_t *opt; message = malloc0(len); opt = (uint8_t *)(message + 1); opt[0] = 99; opt[1] = 130; opt[2] = 83; opt[3] = 99; if (options && optlen) memcpy(&opt[4], options, optlen); if (file && filelen <= 128) memcpy(&message->file, file, filelen); if (sname && snamelen <= 64) memcpy(&message->sname, sname, snamelen); return message; }
sd_bus_slot *bus_slot_allocate( sd_bus *bus, bool floating, BusSlotType type, size_t extra, void *userdata) { sd_bus_slot *slot; assert(bus); slot = malloc0(offsetof(sd_bus_slot, reply_callback) + extra); if (!slot) return NULL; slot->n_ref = 1; slot->type = type; slot->bus = bus; slot->floating = floating; slot->userdata = userdata; if (!floating) sd_bus_ref(bus); LIST_PREPEND(slots, bus->slots, slot); return slot; }
/* ** _ydom_cdata_hdlr() ** DOM handler for SAX parsing */ static void _ydom_cdata_hdlr(ysax_t *sax, char *content) { ydom_t *dom; ydom_node_t *node; YLOG_MOD("ydom", YLOG_DEBUG, "Entering"); dom = (ydom_t*)YSAX_DATA(sax); if (!(node = malloc0(sizeof(ydom_node_t)))) { YLOG_MOD("ydom", YLOG_DEBUG, "Unable to allocate memory"); return ; } node->node_type = CDATA_SECTION_NODE; node->value = content; node->line_nbr = sax->line_nbr; node->complete = YTRUE; if (dom->current_parsed_node->node_type == TEXT_NODE) { dom->current_parsed_node->complete = YTRUE; _ydom_add_next_to_node(dom->current_parsed_node, node); dom->current_parsed_node = dom->current_parsed_node->parent; } else _ydom_add_child_to_node(dom->current_parsed_node, node); YLOG_MOD("ydom", YLOG_DEBUG, "Exiting"); }
void WriteScaledAudioFile (void* arg) { typedef struct { int n; double* ddata; } *dstr; dstr dstruct = (dstr)arg; FILE* file = fopen("AudioFile", "wb"); int i; double max = 0.0; double abs_val; const double conv = 2147483647.0; int *idata = (int *) malloc0 (dstruct->n * sizeof (int)); for (i = 0; i < dstruct->n; i++) { abs_val = fabs (dstruct->ddata[i]); if (abs_val > max) max = abs_val; } for (i = 0; i < dstruct->n; i++) idata[i] = dstruct->ddata[i] >= 0.0 ? (int)floor(conv * dstruct->ddata[i] / max + 0.5) : (int)ceil(conv * dstruct->ddata[i] / max - 0.5); fwrite(idata, sizeof(int), dstruct->n, file); fflush(file); fclose(file); _aligned_free (dstruct->ddata); _aligned_free (dstruct); _aligned_free (idata); _endthread(); }
static struct leds_data *get_leds_data(struct udev_device *udevice) { struct leds_data *data; int number; number = get_js_number(udevice); DBG("number %d", number); data = malloc0(sizeof(*data)); if (!data) return NULL; data->bitmap = calc_leds_bitmap(number); if (data->bitmap == 0) { leds_data_destroy(data); return NULL; } /* * It's OK if this fails, set_leds_hidraw() will be used in * case data->syspath_prefix is NULL. */ data->syspath_prefix = get_leds_syspath_prefix(udevice); return data; }
static int client_receive_message_udp(sd_event_source *s, int fd, uint32_t revents, void *userdata) { sd_dhcp_client *client = userdata; _cleanup_free_ DHCPMessage *message = NULL; int buflen = 0, len, r; assert(s); assert(client); r = ioctl(fd, FIONREAD, &buflen); if (r < 0) return r; if (buflen < 0) /* this can't be right */ return -EIO; message = malloc0(buflen); if (!message) return -ENOMEM; len = read(fd, message, buflen); if (len < 0) { log_dhcp_client(client, "could not receive message from UDP " "socket: %m"); return 0; } else if ((size_t)len < sizeof(DHCPMessage)) return 0; return client_handle_message(client, message, len); }
static int lldp_parse_port_id_tlv(tlv_packet *m) { char *str = NULL, *p; uint16_t length; uint8_t subtype; assert_se(lldp_tlv_packet_enter_container(m, LLDP_TYPE_PORT_ID) >= 0); assert_se(tlv_packet_read_u8(m, &subtype) >= 0); switch (subtype) { case LLDP_PORT_SUBTYPE_INTERFACE_NAME: assert_se(tlv_packet_read_string(m, &str, &length) >= 0); p = malloc0(length + 1); strncpy(p, str, length-1); assert_se(streq(p, TEST_LLDP_PORT) == 1); break; default: assert_not_reached("Unhandled option"); } assert_se(lldp_tlv_packet_exit_container(m) >= 0); return 0; }
int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t type) { _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL; const NLType *nl_type; size_t size; int r; r = type_system_get_type(NULL, &nl_type, type); if (r < 0) return r; assert(nl_type->type == NLA_NESTED); r = message_new_empty(rtnl, &m); if (r < 0) return r; size = NLMSG_SPACE(nl_type->size); assert(size >= sizeof(struct nlmsghdr)); m->hdr = malloc0(size); if (!m->hdr) return -ENOMEM; m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; m->container_type_system[0] = nl_type->type_system; m->hdr->nlmsg_len = size; m->hdr->nlmsg_type = type; *ret = m; m = NULL; return 0; }
static int dynamic_user_add(Manager *m, const char *name, int storage_socket[2], DynamicUser **ret) { DynamicUser *d; int r; assert(m); assert(name); assert(storage_socket); r = hashmap_ensure_allocated(&m->dynamic_users, &string_hash_ops); if (r < 0) return r; d = malloc0(offsetof(DynamicUser, name) + strlen(name) + 1); if (!d) return -ENOMEM; strcpy(d->name, name); d->storage_socket[0] = storage_socket[0]; d->storage_socket[1] = storage_socket[1]; r = hashmap_put(m->dynamic_users, d->name, d); if (r < 0) { free(d); return r; } d->manager = m; if (ret) *ret = d; return 0; }
static int client_receive_message_udp(sd_event_source *s, int fd, uint32_t revents, void *userdata) { sd_dhcp_client *client = userdata; _cleanup_free_ DHCPMessage *message = NULL; int buflen = 0, len, r; assert(s); assert(client); r = ioctl(fd, FIONREAD, &buflen); if (r < 0 || buflen <= 0) buflen = sizeof(DHCPMessage) + DHCP_MIN_OPTIONS_SIZE; message = malloc0(buflen); if (!message) return -ENOMEM; len = read(fd, message, buflen); if (len < 0) { log_dhcp_client(client, "could not receive message from UDP " "socket: %s", strerror(errno)); return 0; } else if ((size_t)len < sizeof(DHCPMessage)) return 0; return client_handle_message(client, message, len); }
/* ** _ydom_open_hdlr() ** DOM handler for SAX parsing */ static void _ydom_open_hdlr(ysax_t *sax, char *tag_name, yvect_t attrs) { ydom_t *dom; ydom_node_t *node; ysax_attr_t *pt; YLOG_MOD("ydom", YLOG_DEBUG, "Entering"); dom = (ydom_t*)YSAX_DATA(sax); if (!(node = malloc0(sizeof(ydom_node_t)))) { YLOG_ADD(YLOG_ERR, "Memory alloc error"); return ; } node->node_type = ELEMENT_NODE; node->name = tag_name; node->line_nbr = sax->line_nbr; while ((pt = yv_pop(attrs))) { _ydom_add_attr_to_node(node, pt->name, pt->value); free0(pt); } yv_del(&attrs, NULL, NULL); if (dom->current_parsed_node->node_type == TEXT_NODE) { dom->current_parsed_node->complete = YTRUE; _ydom_add_next_to_node(dom->current_parsed_node, node); } else _ydom_add_child_to_node(dom->current_parsed_node, node); dom->current_parsed_node = node; YLOG_MOD("ydom", YLOG_DEBUG, "Exiting"); }
static DBusMessage *refresh_advertisement(struct advertisement *ad) { struct mgmt_cp_add_advertising *cp; uint8_t param_len; uint8_t *adv_data; size_t adv_data_len; uint32_t flags = 0; DBG("Refreshing advertisement: %s", ad->path); if (ad->type == AD_TYPE_PERIPHERAL) flags = MGMT_ADV_FLAG_CONNECTABLE | MGMT_ADV_FLAG_DISCOV; if (ad->include_tx_power) flags |= MGMT_ADV_FLAG_TX_POWER; adv_data = bt_ad_generate(ad->data, &adv_data_len); if (!adv_data || (adv_data_len > calc_max_adv_len(ad, flags))) { error("Advertising data too long or couldn't be generated."); return g_dbus_create_error(ad->reg, ERROR_INTERFACE ".InvalidLength", "Advertising data too long."); } param_len = sizeof(struct mgmt_cp_add_advertising) + adv_data_len; cp = malloc0(param_len); if (!cp) { error("Couldn't allocate for MGMT!"); free(adv_data); return btd_error_failed(ad->reg, "Failed"); } cp->flags = flags; cp->instance = ad->instance; cp->adv_data_len = adv_data_len; memcpy(cp->data, adv_data, adv_data_len); free(adv_data); if (!mgmt_send(ad->manager->mgmt, MGMT_OP_ADD_ADVERTISING, ad->manager->mgmt_index, param_len, cp, add_advertising_callback, ad, NULL)) { error("Failed to add Advertising Data"); free(cp); return btd_error_failed(ad->reg, "Failed"); } free(cp); return NULL; }
imap_resp_t * imap_resp_alloc (size_t len) { imap_resp_t *resp; if ((resp = malloc0 (sizeof (imap_resp_t))) == NULL) { return NULL; } if ((resp->buf = malloc0 (len)) == NULL) { free (resp->buf); return NULL; } resp->len = len; return resp; }
RESAMPLEF create_resampleF ( int run, int size, float* in, float* out, int in_rate, int out_rate) { RESAMPLEF a = (RESAMPLEF) malloc0 (sizeof (resampleF)); int x, y, z; int i, j, k; int min_rate; double full_rate; double fc; double fc_norm; double* impulse; a->run = run; a->size = size; a->in = in; a->out = out; x = in_rate; y = out_rate; while (y != 0) { z = y; y = x % y; x = z; } a->L = out_rate / x; a->M = in_rate / x; if (in_rate < out_rate) min_rate = in_rate; else min_rate = out_rate; fc = 0.45 * (double)min_rate; full_rate = (double)(in_rate * a->L); fc_norm = fc / full_rate; a->ncoef = (int)(60.0 / fc_norm); a->ncoef = (a->ncoef / a->L + 1) * a->L; a->cpp = a->ncoef / a->L; a->h = (double *) malloc0 (a->ncoef * sizeof (double)); impulse = fir_bandpass (a->ncoef, -fc_norm, +fc_norm, 1.0, 1, 0, (double)a->L); i = 0; for (j = 0; j < a->L; j ++) for (k = 0; k < a->ncoef; k += a->L) a->h[i++] = impulse[j + k]; a->ringsize = a->cpp; a->ring = (double *) malloc0 (a->ringsize * sizeof (double)); a->idx_in = a->ringsize - 1; a->phnum = 0; _aligned_free (impulse); return a; }
imap_info_t * imap_info_alloc (size_t len) { imap_info_t *info; if ((info = malloc0 (sizeof (imap_info_t))) == NULL) { return NULL; } if ((info->buf = malloc0 (len)) == NULL) { free (info); return NULL; } info->len = len; return info; }
bool simply_msg_animate_element_done(SimplyMsg *self, uint32_t id) { size_t length; ElementAnimateDonePacket *packet = malloc0(length = sizeof(*packet)); if (!packet) { return false; } packet->id = id; return add_packet(self, (Packet*) packet, CommandElementAnimateDone, length); }
static bool send_click(SimplyMsg *self, Command type, ButtonId button) { size_t length; ClickPacket *packet = malloc0(length = sizeof(*packet)); if (!packet) { return false; } packet->button = button; return add_packet(self, (Packet*) packet, type, length); }
void calc_resample (RESAMPLE a) { int x, y, z; int i, j, k; int min_rate; double full_rate; double fc_norm_high, fc_norm_low; double* impulse; a->fc = a->fcin; a->ncoef = a->ncoefin; x = a->in_rate; y = a->out_rate; while (y != 0) { z = y; y = x % y; x = z; } a->L = a->out_rate / x; a->M = a->in_rate / x; if (a->in_rate < a->out_rate) min_rate = a->in_rate; else min_rate = a->out_rate; if (a->fc == 0.0) a->fc = 0.45 * (double)min_rate; full_rate = (double)(a->in_rate * a->L); fc_norm_high = a->fc / full_rate; if (a->fc_low < 0.0) fc_norm_low = - fc_norm_high; else fc_norm_low = a->fc_low / full_rate; if (a->ncoef == 0) a->ncoef = (int)(140.0 * full_rate / min_rate); a->ncoef = (a->ncoef / a->L + 1) * a->L; a->cpp = a->ncoef / a->L; a->h = (double *)malloc0(a->ncoef * sizeof(double)); impulse = fir_bandpass(a->ncoef, fc_norm_low, fc_norm_high, 1.0, 1, 0, a->gain * (double)a->L); i = 0; for (j = 0; j < a->L; j++) for (k = 0; k < a->ncoef; k += a->L) a->h[i++] = impulse[j + k]; a->ringsize = a->cpp; a->ring = (double *)malloc0(a->ringsize * sizeof(complex)); a->idx_in = a->ringsize - 1; a->phnum = 0; _aligned_free(impulse); }