コード例 #1
0
ファイル: serial.c プロジェクト: jcmfernandes/ck
static void *
set_get(const char *value)
{
	unsigned long h;
	void *v;

	h = CK_HS_HASH(&hs, hs_hash, value);
	v = ck_hs_get(&hs, h, value);
	return v;
}
コード例 #2
0
ファイル: mtev_hash.c プロジェクト: postwait/libmtev
int mtev_hash_retrieve(mtev_hash_table *h, const char *k, int klen, void **data) {
  long hashv;
  ck_key_t *retrieved_key;
  union {
    ck_key_t key;
    char pad[sizeof(ck_key_t) + ONSTACK_KEY_SIZE];
  } onstack_key;
  ck_key_t *key = &onstack_key.key;
  ck_hash_attr_t *data_struct;

  if(!h) return 0;
  if(h->u.hs.hf == NULL) {
    mtevL(mtev_error, "warning: null hashtable in mtev_hash_retrieve... initializing\n");
    mtev_stacktrace(mtev_error);
    mtev_hash_init(h);
  }

  if(klen > ONSTACK_KEY_SIZE) key = calloc(1, sizeof(ck_key_t) + klen + 1);
  memcpy(key->label, k, klen);
  key->label[klen] = 0;
  key->len = klen + sizeof(uint32_t);;
  hashv = CK_HS_HASH(&h->u.hs, hs_hash, key);
  retrieved_key = ck_hs_get(&h->u.hs, hashv, key);
  if (retrieved_key) {
    data_struct = index_attribute_container(retrieved_key);
    if (data) {
      if (data_struct) {
        *data = data_struct->data;
      }
      else {
        *data = NULL;
      }
    }
    if(key != &onstack_key.key) free(key);
    return 1;
  }
  if(key != &onstack_key.key) free(key);
  return 0;
}
コード例 #3
0
ファイル: serial.c プロジェクト: 333777/trafficserver
static void
run_test(unsigned int is, unsigned int ad)
{
	ck_hs_t hs[16];
	const size_t size = sizeof(hs) / sizeof(*hs);
	size_t i, j;
	const char *blob = "#blobs";
	unsigned long h;

	if (ck_hs_init(&hs[0], CK_HS_MODE_SPMC | CK_HS_MODE_OBJECT | ad, hs_hash, hs_compare, &my_allocator, is, 6602834) == false)
		ck_error("ck_hs_init\n");

	for (j = 0; j < size; j++) {
		for (i = 0; i < sizeof(test) / sizeof(*test); i++) {
			h = test[i][0];
			if (ck_hs_get(&hs[j], h, test[i]) != NULL) {
				continue;
			}

			if (ck_hs_put_unique(&hs[j], h, test[i]) == false)
				ck_error("ERROR [%zu]: Failed to insert unique (%s)\n", j, test[i]);

			if (ck_hs_remove(&hs[j], h, test[i]) == false)
				ck_error("ERROR [%zu]: Failed to remove unique (%s)\n", j, test[i]);

			break;
		}

		if (ck_hs_gc(&hs[j], 0, 0) == false)
			ck_error("ERROR: Failed to GC empty set.\n");

		for (i = 0; i < sizeof(test) / sizeof(*test); i++) {
			h = test[i][0];
			ck_hs_put(&hs[j], h, test[i]);
			if (ck_hs_put(&hs[j], h, test[i]) == true) {
				ck_error("ERROR [%u] [1]: put must fail on collision (%s).\n", is, test[i]);
			}
			if (ck_hs_get(&hs[j], h, test[i]) == NULL) {
				ck_error("ERROR [%u]: get must not fail after put\n", is);
			}
		}

		/* Test grow semantics. */
		ck_hs_grow(&hs[j], 128);
		for (i = 0; i < sizeof(test) / sizeof(*test); i++) {
			h = test[i][0];
			if (ck_hs_put(&hs[j], h, test[i]) == true) {
				ck_error("ERROR [%u] [2]: put must fail on collision.\n", is);
			}

			if (ck_hs_get(&hs[j], h, test[i]) == NULL) {
				ck_error("ERROR [%u]: get must not fail\n", is);
			}
		}

		h = blob[0];
		if (ck_hs_get(&hs[j], h, blob) == NULL) {
			if (j > 0)
				ck_error("ERROR [%u]: Blob must always exist after first.\n", is);

			if (ck_hs_put(&hs[j], h, blob) == false) {
				ck_error("ERROR [%u]: A unique blob put failed.\n", is);
			}
		} else {
			if (ck_hs_put(&hs[j], h, blob) == true) {
				ck_error("ERROR [%u]: Duplicate blob put succeeded.\n", is);
			}
		}

		/* Grow set and check get semantics. */
		ck_hs_grow(&hs[j], 512);
		for (i = 0; i < sizeof(test) / sizeof(*test); i++) {
			h = test[i][0];
			if (ck_hs_get(&hs[j], h, test[i]) == NULL) {
				ck_error("ERROR [%u]: get must not fail\n", is);
			}
		}

		/* Delete and check negative membership. */
		for (i = 0; i < sizeof(test) / sizeof(*test); i++) {
			void *r;

			h = test[i][0];
			if (ck_hs_get(&hs[j], h, test[i]) == NULL)
				continue;

			if (r = ck_hs_remove(&hs[j], h, test[i]), r == NULL) {
				ck_error("ERROR [%u]: remove must not fail\n", is);
			}

			if (strcmp(r, test[i]) != 0) {
				ck_error("ERROR [%u]: Removed incorrect node (%s != %s)\n", (char *)r, test[i], is);
			}
		}

		/* Test replacement semantics. */
		for (i = 0; i < sizeof(test) / sizeof(*test); i++) {
			void *r;
			bool d;

			h = test[i][0];
			d = ck_hs_get(&hs[j], h, test[i]) != NULL;
			if (ck_hs_set(&hs[j], h, test[i], &r) == false) {
				ck_error("ERROR [%u]: Failed to set\n", is);
			}

			/* Expected replacement. */
			if (d == true && (r == NULL || strcmp(r, test[i]) != 0)) {
				ck_error("ERROR [%u]: Incorrect previous value: %s != %s\n",
				    is, test[i], (char *)r);
			}

			/* Replacement should succeed. */
			if (ck_hs_fas(&hs[j], h, test[i], &r) == false)
				ck_error("ERROR [%u]: ck_hs_fas must succeed.\n", is);

			if (strcmp(r, test[i]) != 0) {
				ck_error("ERROR [%u]: Incorrect replaced value: %s != %s\n",
				    is, test[i], (char *)r);
			}

			if (ck_hs_fas(&hs[j], h, negative, &r) == true)
				ck_error("ERROR [%u]: Replacement of negative should fail.\n", is);

			if (ck_hs_set(&hs[j], h, test[i], &r) == false) {
				ck_error("ERROR [%u]: Failed to set [1]\n", is);
			}

			if (strcmp(r, test[i]) != 0) {
				ck_error("ERROR [%u]: Invalid &hs[j]: %s != %s\n", (char *)r, test[i], is);
			}
		}

		if (j == size - 1)
			break;

		if (ck_hs_move(&hs[j + 1], &hs[j], hs_hash, hs_compare, &my_allocator) == false)
			ck_error("Failed to move hash table");

		if (j & 1) {
			ck_hs_gc(&hs[j + 1], 0, 0);
		} else {
			ck_hs_gc(&hs[j + 1], 26, 26);
		}

		if (ck_hs_rebuild(&hs[j + 1]) == false)
			ck_error("Failed to rebuild");
	}

	return;
}