/* * sgs_session_impl_create() */ sgs_session_impl *sgs_session_impl_create(sgs_connection_impl *connection) { sgs_session_impl *session; session = malloc(sizeof(struct sgs_session_impl)); if (session == NULL) return NULL; /** * The map key (a channel ID) is just a pointer into the map value (an * sgs_channel struct), so we don't need an explicit deallocation function * for the key, just the value. Also note that we have to cast our function * pointers to make them look like they take void* arguments instead of * their usual argument types. */ session->channels = sgs_map_create((int(*)(const void*, const void*))sgs_id_compare); if (session->channels == NULL) { /** roll back allocation of session */ free(session); return NULL; } session->connection = connection; session->session_id = NULL; session->reconnect_key = NULL; session->seqnum_hi = 0; session->seqnum_lo = 0; return session; }
/* * function: main() */ int main(int argc, char *argv[]) { int lookup_key; char str1[] = "foobar"; char str2[] = "chicken soup"; const void *pbuf; int *pkey; int *pkey2; int result; sgs_map *map; map = sgs_map_create(compare_ints); if (map == NULL) { fprintf(stderr, "Could not create sgs_map.\n"); exit(-1); } pkey = malloc(sizeof(int)); *pkey = 100; pbuf = sgs_map_get(map, pkey); print_get(*pkey, pbuf); result = sgs_map_put(map, pkey, str1); printf("Added element {%d, %s}. result=%d\n", *pkey, str1, result); pbuf = sgs_map_get(map, pkey); print_get(*pkey, pbuf); pkey2 = malloc(sizeof(int)); *pkey2 = 200; pbuf = sgs_map_get(map, pkey2); print_get(*pkey2, pbuf); result = sgs_map_put(map, pkey2, str2); printf("Added element {%d, %s}. result=%d\n", *pkey2, str2, result); pbuf = sgs_map_get(map, pkey2); print_get(*pkey2, pbuf); lookup_key = 100; pbuf = sgs_map_get(map, &lookup_key); print_get(lookup_key, pbuf); lookup_key = 300; result = sgs_map_remove(map, &lookup_key); printf("REMOVE(%d) == %d\n", lookup_key, result); lookup_key = 100; result = sgs_map_remove(map, &lookup_key); printf("REMOVE(%d) == %d\n", lookup_key, result); lookup_key = 100; pbuf = sgs_map_get(map, &lookup_key); print_get(lookup_key, pbuf); lookup_key = 200; pbuf = sgs_map_get(map, &lookup_key); print_get(lookup_key, pbuf); sgs_map_clear(map); printf("EMPTY()\n"); lookup_key = 100; pbuf = sgs_map_get(map, &lookup_key); print_get(lookup_key, pbuf); lookup_key = 200; pbuf = sgs_map_get(map, &lookup_key); print_get(lookup_key, pbuf); sgs_map_destroy(map); free(pkey2); free(pkey); printf("Goodbye!\n"); return 0; }