void mp_perfdata_int3(const char *label, long int value, const char *unit, int have_warn, long int warn, int have_crit, long int crit, int have_min, long int min, int have_max, long int max) { thresholds *threshold = NULL; if (have_warn || have_crit) { threshold = mp_malloc(sizeof(thresholds)); memset(threshold, 0, sizeof(thresholds)); if (have_warn) { threshold->warning = mp_malloc(sizeof(range)); memset(threshold->warning, 0, sizeof(range)); threshold->warning->start_infinity = 1; threshold->warning->end = (double) warn; } if (have_crit) { threshold->critical = mp_malloc(sizeof(range)); memset(threshold->critical, 0, sizeof(range)); threshold->critical->start_infinity = 1; threshold->critical->end = (double) crit; } } mp_perfdata_int2(label, value, unit, threshold, have_min, min, have_max, max); free_threshold(threshold); }
bonding_info *parseBond(const char *filename) { FILE *input; char buffer[256]; char *key, *value; int count = 0; bonding_info *info; input = fopen(filename, "r"); if (input == NULL) return NULL; info = mp_malloc(sizeof(bonding_info)); info->slave = NULL; while (fgets(buffer, 256, input) != NULL) { value = buffer; key = strsep(&value, ":"); if(!value) continue; value++; value[strlen(value)-1] = '\0'; if (strcmp(key, "Ethernet Channel Bonding Driver") == 0) { info->version = strdup(value); } else if (strcmp(key, "Bonding Mode") == 0) { info->mode = strdup(value); } else if (strcmp(key, "MII Status") == 0) { if (strcmp(value, "up") == 0) { if(count) info->slave[count-1]->mii_status = 1; else info->mii_status = 1; } else { if(count) info->slave[count-1]->mii_status = 0; else info->mii_status = 0; } } else if (strcmp(key, "Slave Interface") == 0) { count++; info->slaves = count; info->slave = mp_realloc(info->slave, (count+1)*sizeof(bonding_slave_info *)); info->slave[count] = NULL; info->slave[count-1] = mp_malloc(sizeof(struct bonding_slave_info_s)); info->slave[count-1]->interface = strdup(value); } } fclose(input); return info; }
/* * Implementation */ void mpr_init(mpr *op) { op->num.digs = mp_malloc(sizeof(BNS)); op->num.sign = 0; op->num.size = op->num.alloc = 1; op->num.digs[0] = 0; op->den.digs = mp_malloc(sizeof(BNS)); op->den.sign = 0; op->den.size = op->den.alloc = 1; op->den.digs[0] = 1; }
/** * Put an element on synchronized queue. * * @param object element */ void squeue_push(squeue_t* sq, void* object) { squeue_el_t* el = mp_malloc(sizeof(squeue_el_t)); boolean_t notify = B_FALSE; el->s_next = NULL; el->s_data = object; mutex_lock(&sq->sq_mutex); /* */ assert(!(sq->sq_head == NULL && sq->sq_tail != NULL)); if(sq->sq_head == NULL) { /* Empty queue, notify pop*/ sq->sq_head = el; notify = B_TRUE; } else if(sq->sq_tail == NULL) { /* Only sq_head present, insert el as tail and link head with tail */ sq->sq_tail = el; sq->sq_head->s_next = el; } else { /* Add element and move tail forward */ sq->sq_tail->s_next = el; sq->sq_tail = el; } mutex_unlock(&sq->sq_mutex); if(notify) cv_notify_one(&sq->sq_cv); }
char *sms_encode_number(const char *number) { int i; int len; char *n; char *dest; n = (char *)number; len = strlen(number); if (number[0] == '+') { len -= 1; n++; } len += len%2; dest = mp_malloc(len + 3); memset(dest, 0, len + 3); if (number[0] == '+') strcpy(dest, "91"); else strcpy(dest, "81"); for (i=0; i < len; i+=2) { dest[i+3] = n[i]; dest[i+2] = n[i+1] != '\0' ? n[i+1] : 'F'; } return dest; }
PLATAPI cpumask_t* cpumask_create(void) { cpumask_t* mask = (cpumask_t*) mp_malloc(sizeof(cpumask_t)); cpumask_reset(mask); return mask; }
char *mp_human_size(float size) { char *out; int exp; for(exp = 0; exp < 5 && size > 1024; exp++) { size /= 1024; } out = mp_malloc(14); sprintf(out, "%.2f ", size); switch (exp) { case 1: strcat(out, "KiB"); break; case 2: strcat(out, "MiB"); break; case 3: strcat(out, "GiB"); break; case 4: strcat(out, "TiB"); break; } return out; }
json_node_t* json_tsfile_get_array(tsfile_t* file, unsigned start, unsigned end) { json_node_t** nodes = NULL; json_node_t* node_array = NULL; unsigned long entry_size = file->header->schema.hdr.entry_size; void* entries = NULL; void* entry; int count = end - start; int ni; if(count <= 0) { tsfile_errno = TSFILE_INVAL_RANGE; return NULL; } entries = mp_malloc(entry_size * count); if(tsfile_get_entries(file, entries, start, end) != TSFILE_OK) { goto end; } nodes = tsfile_get_nodes(file, count); node_array = json_new_array(); for(ni = 0; ni < count; ++ni) { entry = ((char*) entries) + entry_size * ni; tsfile_fill_node(file, nodes[ni], entry); json_add_node(node_array, NULL, nodes[ni]); } end: if(nodes) mp_free(nodes); mp_free(entries); return node_array; }
int mp_snmp_values_fetch2(netsnmp_session *ss, const mp_snmp_value *values) { const mp_snmp_value *vp1; mp_snmp_query_cmd *vp2; size_t count; mp_snmp_query_cmd *oid_values = NULL; int rc = 0; for (count = 0, vp1 = values; vp1->oid; vp1++, count++) ; oid_values = (mp_snmp_query_cmd *) mp_malloc(count * sizeof(mp_snmp_query_cmd)); for (vp1 = values, vp2 = oid_values; vp1->oid; vp1++, vp2++) { vp2->oid_len = MAX_OID_LEN; if (!read_objid(vp1->oid, vp2->oid, &vp2->oid_len)) { if (mp_verbose > 3) printf("Invalid OID: %s\n", vp1->oid); goto done; } vp2->type = vp1->type; vp2->target = vp1->target; vp2->target_len = vp1->target_len; } rc = mp_snmp_values_fetch1(ss, oid_values); done: free(oid_values); return rc; }
/* * Implementation */ void mpi_init(mpi *op) { op->sign = 0; op->size = op->alloc = 1; op->digs = mp_malloc(sizeof(BNS)); op->digs[0] = 0; }
module_t* mod_create() { module_t* mod = (module_t*) mp_malloc(sizeof(module_t)); mod->mod_status = MOD_UNITIALIZED; mod->mod_next = NULL; return mod; }
END_TEST // mp_malloc START_TEST (test_malloc_ok) { char *dest; dest = mp_malloc(10); free(dest); }
PLATAPI plat_dir_t* plat_opendir(const char *name) { plat_dir_t* dirp = (plat_dir_t*) mp_malloc(sizeof(plat_dir_t)); dirp->d_handle = NULL; strncpy(dirp->d_path, name, MAX_PATH); /* Should find all files in directory */ strncat(dirp->d_path, "\\*", MAX_PATH); return dirp; }
int json_tsfile_add(tsfile_t* file, json_node_t* node) { unsigned long entry_size = file->header->schema.hdr.entry_size; void* entry = mp_malloc(entry_size); int ret; tsfile_fill_entry(file, node, entry); ret = tsfile_add(file, entry, 1); mp_free(entry); return ret; }
static int copy_value(const netsnmp_variable_list *var, const u_char type, size_t target_len, void **target) { if (var->type != type) { if (mp_verbose > 1) printf("TYPE Mismatch: 0x%X ~ 0x%X\n", var->type, type); return 0; } switch(var->type) { case ASN_INTEGER: // 0x02 /* FALL-TROUGH */ case ASN_COUNTER: // 0x41 /* FALL-TROUGH */ case ASN_GAUGE: // 0x42 /* FALL-TROUGH */ case ASN_TIMETICKS: if (var->val_len > target_len) { if (mp_verbose > 1) printf("TARGET size mismatch: provided storage " "to small (have %zu, need %zu)\n", target_len, var->val_len); return 0; } else { memcpy(target, var->val.integer, var->val_len); } break; case ASN_OCTET_STR: // 0x04 { if (target_len > 0) { if ((var->val_len + 1) > target_len) { if (mp_verbose > 1) printf("TARGET size mismatch: provided storage " "to small (have %zu, need %zu)\n", target_len, var->val_len); return 0; } } else { char *buffer; buffer = mp_malloc(var->val_len + 1); *target = (void*) buffer; } memcpy(*target, var->val.string, var->val_len); ((char *) *target)[var->val_len] = '\0'; } break; default: printf("TYPE Mismatch: unexpected type 0x%X\n", var->type); return 0; } /* switch */ return 1; }
PLATAPI plat_dir_t* plat_opendir(const char *name) { plat_dir_t* dir; DIR* d_dirp = opendir(name); if(d_dirp == NULL) return NULL; dir = mp_malloc(sizeof(plat_dir_t)); dir->d_dirp = d_dirp; strncpy(dir->d_path, name, PATHMAXLEN); return dir; }
/* We should malloc from a buffer pool */ void* _smalloc(const char *func_name, int line, int size) { void *rv; CSF_UNUSED_ARG(func_name); CSF_UNUSED_ARG(line); rv = mp_malloc(size); //rv = calloc(1, size); if(rv == NULL) _crit(__func__, __LINE__, "Memory allocation error."); return rv; }
tsfile_t* tsfile_create(const char* filename, tsfile_schema_t* schema) { tsfile_t* file = NULL; tsfile_header_t* header = NULL; size_t schema_size = sizeof(tsfile_shdr_t) + schema->hdr.count * sizeof(tsfile_field_t); if(!tsfile_check_schema(schema)) { return NULL; } file = tsfile_open_file(filename, B_TRUE); if(file == NULL) return NULL; /* Initialize header */ header = mp_malloc(TSFILE_HEADER_SIZE); memcpy(&header->magic, TSFILE_MAGIC, TSFILE_MAGIC_LEN); header->version = TSFILE_VERSION; memset(&header->sb[1], 0, sizeof(tsfile_sb_t) * (SBCOUNT - 1)); TSFILE_SB_SET_COUNT(header, 0, 0, 0); memset(&header->schema, 0, sizeof(tsfile_schema_t)); memcpy(&header->schema, schema, schema_size); memset(header + 1, 0, TSFILE_HEADER_SIZE - sizeof(tsfile_header_t)); if(write(file->fd, header, TSFILE_HEADER_SIZE) < TSFILE_HEADER_SIZE) { tsfile_error_msg(TSE_INTERNAL_ERROR, "Failed to write header of '%s'", filename); mp_free(header); tsfile_close_file(file); return NULL; } file->header = header; file->cur_sb = 0ul; file->sb_diff = 0; mutex_init(&file->mutex, "tsfile-%p", file); tsfile_init_nodes(file); return file; }
static tsfile_t* tsfile_open_file(const char* filename, boolean_t create) { tsfile_t* file = NULL; int fd; size_t filename_len; int flags = O_RDWR; if(tsfile_sync_mode) flags |= O_SYNC; #ifdef _MSC_VER flags |= _O_BINARY; #endif if(create) { fd = open(filename, flags | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); } else { fd = open(filename, flags); } if(fd == -1) { tsfile_error_msg(TSE_INTERNAL_ERROR, "Failed to open tsfile '%s'!", filename); return NULL; } file = mp_cache_alloc(&tsfile_cache); file->fd = fd; filename_len = strlen(filename) + 1; file->filename = mp_malloc(filename_len); strncpy(file->filename, filename, filename_len); if(create) { file->size = 0; } else { file->size = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); } file->header = NULL; return file; }
void mp_dhcp_pkt_opt(struct dhcp_pkt *pkt, uint8_t code, uint8_t len, char *data) { if(pkt->optlen == 0) { pkt->opts = mp_malloc(len + 3); pkt->optlen = 1; } else { pkt->opts = mp_realloc(pkt->opts, pkt->optlen + len + 2); } pkt->opts[(pkt->optlen)-1] = code; if (len) { pkt->opts[pkt->optlen] = len; memcpy(&pkt->opts[pkt->optlen+1], data, len); pkt->optlen += 2 + len; } pkt->opts[(pkt->optlen)-1] = DHCPOPT_End; }
struct rpcent *rpc_getrpcent(const char *prog) { struct rpcent *ent, *ret; if (isalpha(*prog)) { ent = getrpcbyname((char *)prog); } else { ent = getrpcbynumber(atoi(prog)); } if (ent == NULL) return NULL; ret = mp_malloc(sizeof(struct rpcent)); ret->r_name = mp_strdup(ent->r_name); ret->r_number = ent->r_number; return ret; }
char * mpr_getstr(char *str, mpr *op, int base) { int len; if (str == NULL) { len = mpi_getsize(mpr_num(op), base) + mpr_num(op)->sign + 1 + mpi_getsize(mpr_den(op), base) + mpr_den(op)->sign + 1; str = mp_malloc(len); } (void)mpi_getstr(str, mpr_num(op), base); len = strlen(str); str[len] = '/'; (void)mpi_getstr(str + len + 1, mpr_den(op), base); return (str); }
void* csv_entry_create(list_head_t* list, size_t entry_size) { csv_entry_chunk_t* entry_chunk; if(!list_empty(list)) { entry_chunk = list_last_entry(csv_entry_chunk_t, list, node); if(entry_chunk->count < csv_entries_per_chunk) return &entry_chunk->byte + (entry_chunk->count++ * entry_size); } entry_chunk = mp_malloc(sizeof(csv_entry_chunk_t) + entry_size * csv_entries_per_chunk); list_node_init(&entry_chunk->node); entry_chunk->count = 1; list_add_tail(&entry_chunk->node, list); return &entry_chunk->byte; }
int mp_asprintf(char **retp, const char *format, ...) { int len=0; va_list ap; char *buf; // Estimate len va_start(ap, format); len = vsnprintf(NULL, 0, format, ap); va_end(ap); // Get buffer buf = mp_malloc(len + 1); // sprintf va_start(ap, format); vsnprintf(buf, len+1, format, ap); va_end(ap); *retp = buf; return len; }
int mp_snmp_values_fetch3(netsnmp_session *ss, const mp_snmp_value *values, ...) { va_list ap; const mp_snmp_value *vp1; mp_snmp_query_cmd *vp2; size_t count; char formatted_oid[1024]; mp_snmp_query_cmd *oid_values = NULL; int rc = 0; for (count = 0, vp1 = values; vp1->oid; vp1++, count++) ; oid_values = (mp_snmp_query_cmd *) mp_malloc(count * sizeof(mp_snmp_query_cmd)); for (vp1 = values, vp2 = oid_values; vp1->oid; vp1++, vp2++) { va_start(ap, values); vsnprintf(formatted_oid, sizeof(formatted_oid), vp1->oid, ap); va_end(ap); vp2->oid_len = MAX_OID_LEN; if (!read_objid(formatted_oid, vp2->oid, &vp2->oid_len)) { if (mp_verbose > 3) printf("Invalid OID: %s\n", vp1->oid); goto done; } vp2->type = vp1->type; vp2->target = vp1->target; vp2->target_len = vp1->target_len; } rc = mp_snmp_values_fetch1(ss, oid_values); done: free(oid_values); return rc; }
json_node_t* json_tsfile_get(tsfile_t* file, unsigned number) { json_node_t** nodes = NULL; unsigned long entry_size = file->header->schema.hdr.entry_size; void* entry = mp_malloc(entry_size); json_node_t* node = NULL; if(tsfile_get_entries(file, entry, number, number + 1) != TSFILE_OK) { goto end; } nodes = tsfile_get_nodes(file, 1); node = nodes[0]; tsfile_fill_node(file, node, entry); end: if(nodes) mp_free(nodes); mp_free(entry); return node; }
/* * @brief alloc a continuous memory block for list storage * @param pp_mem_base -i/o- pointer to the address of memory base if success * max_node_num -i- number of node entities user wants to alloc * @return return a pointer to the memory head if success, else a NULL */ struct mp_mem_head_t *mp_alloc(mp_i32 **pp_mem_base, mp_u32 max_node_num) { struct mp_mem_head_t *p_mem_head = NULL; mp_u32 size = 0; size = max_node_num * sizeof(struct mp_node_t) + sizeof(struct mp_mem_head_t); /* FIXME check this size? */ *pp_mem_base = (mp_i32 *)mp_malloc(size); if (*pp_mem_base == NULL) return NULL; memset(*pp_mem_base, 0, size); p_mem_head = (struct mp_mem_head_t *)*pp_mem_base; p_mem_head->p_node_first = NULL; p_mem_head->size = size; p_mem_head->used_node_num = 0; p_mem_head->max_node_num = max_node_num; #ifdef DEBUG_PRINT_ON printf("memory pool info\n"); printf("------------------------\n"); printf("p_mem_base: %p\np_node_first: %p\np_node_tail: %p\n" "total size: %d\nsize of head: %u\nsize of node: %u\n" "max node number: %u\nused node number: %u\n", *pp_mem_base, p_mem_head->p_node_first, p_mem_head->p_node_tail, p_mem_head->size, (mp_u32)sizeof(struct mp_mem_head_t), (mp_u32)sizeof(struct mp_node_t), p_mem_head->max_node_num, p_mem_head->used_node_num); printf("------------------------\n"); #endif return p_mem_head; }
char *sms_encode_pdu(const char *smsc, const char *number, const char *text) { char *pdu; char *encSmsc = NULL; char *encNumber = NULL; char *encText = NULL; int len; len = 14; if (smsc) { encSmsc = sms_encode_number(smsc); len += strlen(encSmsc); } encNumber = sms_encode_number(number); len += strlen(encNumber); encText = sms_encode_text(text); len += strlen(encText); pdu = mp_malloc(len+1); memset(pdu, 0, len+1); if (smsc) { mp_asprintf(&pdu, "%02X%s0500%02X%s0000%s", strlen(encSmsc)/2, encSmsc, strlen(encNumber)-2, encNumber, encText); free(encSmsc); } else { mp_asprintf(&pdu, "000500%02X%s0000%s", strlen(encNumber)-2, encNumber, encText); } free(encNumber); free(encText); return pdu; }
/** * Parse bitmap using `hi_linux_sysfs_readbitmap()` and call function `proc()` * for all bits that are set to 1. * * @param count maximum number of bits * @param proc walker function that receives bit id * @param arg argument passed as second argument to `proc()` * * @return HI_LINUX_SYSFS_OK if everything went fine or HI_LINUX_SYSFS_ERROR if \ * reading failed or overflow occured. * * @see hi_linux_sysfs_readbitmap */ int hi_linux_sysfs_walkbitmap(const char* root, const char* name, const char* object, int count, void (*proc)(int id, void* arg), void* arg) { int len = count / 32; uint32_t* bitmap = mp_malloc(len * sizeof(uint32_t)); int i = 0; int ret = 0; ret = hi_linux_sysfs_readbitmap(root, name, object, bitmap, len); if(ret != HI_LINUX_SYSFS_OK) { mp_free(bitmap); return HI_LINUX_SYSFS_ERROR; } hi_sysfs_dprintf("hi_linux_sysfs_walkbitmap: %s/%s/%s => [%x, ...]\n", root, name, object, bitmap[0]); for(i = 0; i < count; ++i) { if(bitmap[i / 32] & (1 << (i % 32))) proc(i, arg); } mp_free(bitmap); return HI_LINUX_SYSFS_OK; }
int json_tsfile_add_array(tsfile_t* file, json_node_t* node_array) { unsigned long entry_size = file->header->schema.hdr.entry_size; void* entries = NULL; void* entry; int count = json_size(node_array); int ni = 0; int ret; json_node_t* node = json_first(node_array, &ni); entries = mp_malloc(entry_size * count); while(!json_is_end(node_array, node, &ni)) { entry = ((char*) entries) + entry_size * ni; tsfile_fill_entry(file, node, entry); node = json_next(node, &ni); } ret = tsfile_add(file, entries, count); mp_free(entries); return ret; }