2016-11-23 19:23:33 +00:00
|
|
|
#include "ks.h"
|
|
|
|
|
2015-09-04 20:06:52 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2016-11-23 19:23:33 +00:00
|
|
|
#include "tap.h"
|
2017-02-17 02:34:43 +00:00
|
|
|
#define STR "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
2016-11-23 19:23:33 +00:00
|
|
|
|
|
|
|
static void fill(char *str, int bytes, char c)
|
|
|
|
{
|
|
|
|
memset(str, c, bytes -1);
|
|
|
|
*(str+(bytes-1)) = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
struct foo {
|
|
|
|
int x;
|
|
|
|
char *str;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void cleanup(ks_pool_t *mpool, void *ptr, void *arg, int type, ks_pool_cleanup_action_t action, ks_pool_cleanup_type_t ctype)
|
|
|
|
{
|
|
|
|
struct foo *foo = (struct foo *) ptr;
|
|
|
|
|
|
|
|
printf("Cleanup %p action: %d\n", ptr, action);
|
|
|
|
|
|
|
|
switch(action) {
|
|
|
|
case KS_MPCL_ANNOUNCE:
|
|
|
|
break;
|
|
|
|
case KS_MPCL_TEARDOWN:
|
|
|
|
break;
|
|
|
|
case KS_MPCL_DESTROY:
|
|
|
|
printf("DESTROY STR [%s]\n", foo->str);
|
|
|
|
free(foo->str);
|
|
|
|
foo->str = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-04 20:06:52 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2016-11-23 19:23:33 +00:00
|
|
|
ks_pool_t *pool;
|
2015-09-04 20:06:52 +00:00
|
|
|
int err = 0;
|
|
|
|
char *str = NULL;
|
|
|
|
int bytes = 1024;
|
2016-11-23 19:23:33 +00:00
|
|
|
ks_status_t status;
|
|
|
|
struct foo *foo;
|
|
|
|
|
|
|
|
ks_init();
|
|
|
|
|
2017-02-17 02:34:43 +00:00
|
|
|
plan(14);
|
2015-09-04 20:06:52 +00:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
int tmp = atoi(argv[1]);
|
|
|
|
|
|
|
|
if (tmp > 0) {
|
|
|
|
bytes = tmp;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "INVALID\n");
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 19:23:33 +00:00
|
|
|
status = ks_pool_open(&pool);
|
2015-09-04 20:06:52 +00:00
|
|
|
|
2016-11-23 19:23:33 +00:00
|
|
|
printf("OPEN:\n");
|
|
|
|
ok(status == KS_STATUS_SUCCESS);
|
|
|
|
if (status != KS_STATUS_SUCCESS) {
|
|
|
|
fprintf(stderr, "OPEN ERR: %d [%s]\n", err, ks_pool_strerror(status));
|
2015-09-04 20:06:52 +00:00
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
2016-11-23 19:23:33 +00:00
|
|
|
printf("ALLOC:\n");
|
|
|
|
str = ks_pool_alloc(pool, bytes);
|
2015-09-04 20:06:52 +00:00
|
|
|
|
2016-11-23 19:23:33 +00:00
|
|
|
ok(str != NULL);
|
|
|
|
if (!str) {
|
|
|
|
fprintf(stderr, "ALLOC ERR\n");
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
fill(str, bytes, '.');
|
2015-09-04 20:06:52 +00:00
|
|
|
printf("%s\n", str);
|
|
|
|
|
2016-11-23 19:23:33 +00:00
|
|
|
printf("FREE:\n");
|
|
|
|
|
2016-12-16 02:16:10 +00:00
|
|
|
status = ks_pool_free(pool, &str);
|
2016-11-23 19:23:33 +00:00
|
|
|
if (status != KS_STATUS_SUCCESS) {
|
|
|
|
fprintf(stderr, "FREE ERR: [%s]\n", ks_pool_strerror(err));
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("ALLOC2:\n");
|
|
|
|
|
|
|
|
str = ks_pool_alloc(pool, bytes);
|
|
|
|
|
|
|
|
ok(str != NULL);
|
|
|
|
if (!str) {
|
|
|
|
fprintf(stderr, "ALLOC2 ERR: [FAILED]\n");
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
2017-02-17 02:34:43 +00:00
|
|
|
|
|
|
|
ks_snprintf(str, bytes, "%s", STR);
|
2016-11-23 19:23:33 +00:00
|
|
|
printf("%s\n", str);
|
|
|
|
|
|
|
|
|
2017-02-17 02:34:43 +00:00
|
|
|
printf("ALLOC3 (refs):\n");
|
|
|
|
|
|
|
|
str = ks_pool_ref(pool, str);
|
|
|
|
|
|
|
|
printf("STR [%s]\n", str);
|
|
|
|
|
|
|
|
ks_pool_free(pool, &str);
|
|
|
|
|
|
|
|
ok(str != NULL && !strcmp(str, STR));
|
|
|
|
|
|
|
|
printf("STR [%s]\n", str);
|
|
|
|
|
|
|
|
ks_pool_free(pool, &str);
|
|
|
|
|
|
|
|
ok(str == NULL);
|
|
|
|
|
|
|
|
str = ks_pool_alloc(pool, bytes);
|
|
|
|
|
|
|
|
ok(str != NULL);
|
|
|
|
if (!str) {
|
|
|
|
fprintf(stderr, "ALLOC2 ERR: [FAILED]\n");
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
fill(str, bytes, '-');
|
|
|
|
printf("%s\n", str);
|
|
|
|
|
2016-11-23 19:23:33 +00:00
|
|
|
printf("ALLOC OBJ:\n");
|
|
|
|
|
|
|
|
foo = ks_pool_alloc(pool, sizeof(struct foo));
|
|
|
|
|
|
|
|
ok(foo != NULL);
|
|
|
|
if (!foo) {
|
|
|
|
fprintf(stderr, "ALLOC OBJ: [FAILED]\n");
|
|
|
|
exit(255);
|
|
|
|
} else {
|
|
|
|
printf("ALLOC OBJ [%p]:\n", (void *) foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
foo->x = 12;
|
|
|
|
foo->str = strdup("This is a test 1234 abcd; This will be called on explicit free\n");
|
|
|
|
ks_pool_set_cleanup(pool, foo, NULL, 0, cleanup);
|
|
|
|
|
|
|
|
printf("FREE OBJ:\n");
|
|
|
|
|
2016-12-16 02:16:10 +00:00
|
|
|
status = ks_pool_free(pool, &foo);
|
2016-11-23 19:23:33 +00:00
|
|
|
ok(status == KS_STATUS_SUCCESS);
|
|
|
|
if (status != KS_STATUS_SUCCESS) {
|
|
|
|
fprintf(stderr, "FREE OBJ ERR: [%s]\n", ks_pool_strerror(status));
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf("ALLOC OBJ2:\n");
|
|
|
|
|
|
|
|
foo = ks_pool_alloc(pool, sizeof(struct foo));
|
|
|
|
|
|
|
|
ok(foo != NULL);
|
|
|
|
if (!foo) {
|
|
|
|
fprintf(stderr, "ALLOC OBJ2: [FAILED]\n");
|
|
|
|
exit(255);
|
|
|
|
} else {
|
|
|
|
printf("ALLOC OBJ2 [%p]:\n", (void *) foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
foo->x = 12;
|
|
|
|
foo->str = strdup("This is a second test 1234 abcd; This will be called on pool clear/destroy\n");
|
|
|
|
ks_pool_set_cleanup(pool, foo, NULL, 0, cleanup);
|
|
|
|
|
|
|
|
|
|
|
|
printf("ALLOC OBJ3:\n");
|
|
|
|
|
|
|
|
foo = ks_pool_alloc(pool, sizeof(struct foo));
|
|
|
|
|
|
|
|
ok(foo != NULL);
|
|
|
|
if (!foo) {
|
|
|
|
fprintf(stderr, "ALLOC OBJ3: [FAILED]\n");
|
2015-09-04 20:06:52 +00:00
|
|
|
exit(255);
|
2016-11-23 19:23:33 +00:00
|
|
|
} else {
|
|
|
|
printf("ALLOC OBJ3 [%p]:\n", (void *) foo);
|
2015-09-04 20:06:52 +00:00
|
|
|
}
|
2016-11-23 19:23:33 +00:00
|
|
|
|
|
|
|
foo->x = 12;
|
|
|
|
foo->str = strdup("This is a third test 1234 abcd; This will be called on pool clear/destroy\n");
|
|
|
|
ks_pool_set_cleanup(pool, foo, NULL, 0, cleanup);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("RESIZE:\n");
|
2017-02-15 02:48:54 +00:00
|
|
|
|
|
|
|
|
2017-02-17 02:34:43 +00:00
|
|
|
ks_snprintf(str, bytes, "%s", STR);
|
2017-02-15 02:48:54 +00:00
|
|
|
printf("1 STR [%s]\n", str);
|
2016-11-23 19:23:33 +00:00
|
|
|
bytes *= 2;
|
|
|
|
str = ks_pool_resize(pool, str, bytes);
|
2017-02-15 02:48:54 +00:00
|
|
|
printf("2 STR [%s]\n", str);
|
|
|
|
|
2017-02-17 02:34:43 +00:00
|
|
|
ok(!strcmp(str, STR));
|
2016-11-23 19:23:33 +00:00
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
fprintf(stderr, "RESIZE ERR: [FAILED]\n");
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
fill(str, bytes, '*');
|
|
|
|
printf("%s\n", str);
|
|
|
|
|
|
|
|
|
|
|
|
printf("FREE 2:\n");
|
|
|
|
|
2016-12-16 02:16:10 +00:00
|
|
|
status = ks_pool_free(pool, &str);
|
2016-11-23 19:23:33 +00:00
|
|
|
ok(status == KS_STATUS_SUCCESS);
|
|
|
|
if (status != KS_STATUS_SUCCESS) {
|
|
|
|
fprintf(stderr, "FREE2 ERR: [%s]\n", ks_pool_strerror(status));
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf("CLEAR:\n");
|
|
|
|
status = ks_pool_clear(pool);
|
|
|
|
|
|
|
|
ok(status == KS_STATUS_SUCCESS);
|
|
|
|
if (status != KS_STATUS_SUCCESS) {
|
|
|
|
fprintf(stderr, "CLEAR ERR: [%s]\n", ks_pool_strerror(status));
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("CLOSE:\n");
|
|
|
|
status = ks_pool_close(&pool);
|
2015-09-04 20:06:52 +00:00
|
|
|
|
2016-11-23 19:23:33 +00:00
|
|
|
ok(status == KS_STATUS_SUCCESS);
|
|
|
|
if (status != KS_STATUS_SUCCESS) {
|
|
|
|
fprintf(stderr, "CLOSE ERR: [%s]\n", ks_pool_strerror(err));
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
ks_shutdown();
|
|
|
|
|
|
|
|
done_testing();
|
2015-09-04 20:06:52 +00:00
|
|
|
}
|