str store_serialize(map_t _store) { cJSON *flat_map; str ret = STR_NULL; if (map_size(_store) == 0) return ret; cJSON_InitHooks(&shm_hooks); flat_map = cJSON_CreateObject(); if (!flat_map) { LM_ERR("oom\n"); return ret; } if (map_for_each(_store, push_kv_to_json, flat_map) != 0) LM_ERR("oom - serialized map is incomplete!\n"); ret.s = cJSON_PrintUnformatted(flat_map); if (!ret.s) { LM_ERR("oom\n"); goto out; } ret.len = strlen(ret.s); out: cJSON_Delete(flat_map); cJSON_InitHooks(NULL); return ret; }
int rl_stats(struct mi_node *rpl, str * value) { rl_pipe_t **pipe; int i; if (value && value->s && value->len) { i = RL_GET_INDEX(*value); RL_GET_LOCK(i); pipe = RL_FIND_PIPE(i, *value); if (!pipe || !*pipe) { LM_DBG("pipe %.*s not found\n", value->len, value->s); goto error; } if (rl_map_print(rpl, *value, *pipe)) { LM_ERR("cannot print value for key %.*s\n", value->len, value->s); goto error; } RL_RELEASE_LOCK(i); } else { /* iterate through each map */ for (i = 0; i < rl_htable.size; i++) { RL_GET_LOCK(i); if (map_for_each(rl_htable.maps[i], rl_map_print, rpl)) { LM_ERR("cannot print values\n"); goto error; } RL_RELEASE_LOCK(i); } } return 0; error: RL_RELEASE_LOCK(i); return -1; }
void plist_print_all(struct map* m) { printf("# %-3s %-16s %-11s %-6s %-6s %-13s\n", "PID", "NAME", "EXIT_STATUS", "ALIVE?", "PARENT", "PARENT_ALIVE?"); lock_acquire(&plist_lock); map_for_each(m, (void*) &plist_print_process, 0); lock_release(&plist_lock); }
int rl_stats(mi_item_t *resp_obj, str * value) { mi_item_t *pipe_item, *pipe_arr; rl_pipe_t **pipe; int i; if (value && value->s && value->len) { i = RL_GET_INDEX(*value); RL_GET_LOCK(i); pipe = RL_FIND_PIPE(i, *value); if (!pipe || !*pipe) { LM_DBG("pipe %.*s not found\n", value->len, value->s); goto error; } pipe_item = add_mi_object(resp_obj, MI_SSTR("Pipe")); if (!pipe_item) goto error; if (rl_map_print(pipe_item, *value, *pipe)) { LM_ERR("cannot print value for key %.*s\n", value->len, value->s); goto error; } RL_RELEASE_LOCK(i); } else { /* iterate through each map */ pipe_arr = add_mi_array(resp_obj, MI_SSTR("Pipes")); if (!pipe_arr) return -1; for (i = 0; i < rl_htable.size; i++) { pipe_item = add_mi_object(pipe_arr, NULL, 0); if (!pipe_item) return -1; RL_GET_LOCK(i); if (map_for_each(rl_htable.maps[i], rl_map_print, pipe_item)) { LM_ERR("cannot print values\n"); goto error; } RL_RELEASE_LOCK(i); } } return 0; error: RL_RELEASE_LOCK(i); return -1; }
int rl_stats(struct mi_root *rpl_tree, str * value) { rl_pipe_t **pipe; struct rl_param_t param; int i; memset(¶m, 0, sizeof(struct rl_param_t)); param.node = &rpl_tree->node; param.root = rpl_tree; if (value && value->s && value->len) { i = RL_GET_INDEX(*value); RL_GET_LOCK(i); pipe = RL_FIND_PIPE(i, *value); if (!pipe || !*pipe) { LM_DBG("pipe %.*s not found\n", value->len, value->s); goto error; } if (rl_map_print(¶m, *value, *pipe)) { LM_ERR("cannot print value for key %.*s\n", value->len, value->s); goto error; } RL_RELEASE_LOCK(i); } else { /* iterate through each map */ for (i = 0; i < rl_htable.size; i++) { RL_GET_LOCK(i); if (map_for_each(rl_htable.maps[i], rl_map_print, ¶m)) { LM_ERR("cannot print values\n"); goto error; } RL_RELEASE_LOCK(i); } } return 0; error: RL_RELEASE_LOCK(i); return -1; }
int main() { struct map container; char input_buffer[10]; char* obj; int id; int i; map_init(&container); /* remember to try to insert more values than you map can hold */ printf("Insert values: "); for ( i = 0; i < LOOPS; ++i) { /* insecure, scanf may overflow the input buffer array * * very serious, but we ignore it in this test program */ scanf("%s", input_buffer); /*! allocates a copy of the input and inserts in map */ obj = my_strdup(input_buffer); id = map_insert(&container, obj); } /* remember to test with invalid keys (like 4711, or -1) */ for ( i = 0; i < LOOPS; ++i) { printf("Enter id to find value for: "); scanf("%d", &id); /*! find the value for a key in the map */ obj = map_find(&container, id); /*! if it was found, display it */ YOUR CODE /* since we leave the value in the map we may use it again and * should not free the memory */ } /* remember to test with invalid keys (like 4711, or -1) */ for ( i = 0; i < LOOPS; ++i) { printf("Enter id to remove value for: "); scanf("%d", &id); /*! find and remove a value for a key in the map */ obj = map_remove(&container, id); /*! if it was found, display it */ YOUR CODE /* since we removed the value from the map we will never use it again and * must properly free the memory (if it was allocated) */ } /*! print all strings representing an integer less than 5 */ printf("Will now display all values less than N. Choose N: "); scanf("%d", &i); map_for_each(&container, print_less, 5); /*! free all remaining memory and remove from map */ map_remove_if(&container, do_free, 0); return 0; }