예제 #1
0
파일: test.c 프로젝트: ifrouter/algorithm
int main()
{
	char *text = "ababababca";
	char *s = "ababca";
	char *result = NULL;
	int i;

	KMP_MATCH *pkmp = kmp_init(s, strlen(s));
	if (pkmp == NULL)
	{
		return 0;
	}


	result = kmp_match(text, strlen(text), pkmp);
	if (result)
	{
		printf("found: offset is %d\n", result-text);
	}
	else
	{
		printf("not found\n");
	}

	kmp_free(pkmp);
	return 0;
}
예제 #2
0
int test_kmp_aligned_malloc()
{
  int err = 0;
  #pragma omp parallel shared(err)
  {
    int i;
    int* ptr;
    uint64_t addr;
    int tid = omp_get_thread_num();

    for(i = 0; i < sizeof(alignments)/sizeof(int); i++) {
      int alignment = alignments[i];
      // allocate 64 bytes with 64-byte alignment
      // allocate 128 bytes with 128-byte alignment, etc.
      ptr = (int*)kmp_aligned_malloc(alignment, alignment);
      addr = (uint64_t)ptr;
      if(addr & (alignment-1)) {
        printf("thread %d: addr = %p (aligned to %u bytes) but expected "
               " alignment = %d\n", tid, ptr, aligned_by(addr), alignment);
        err = 1;
      }
      kmp_free(ptr);
    }

    ptr = kmp_aligned_malloc(128, 127);
    if (ptr != NULL) {
      printf("thread %d: kmp_aligned_malloc() didn't return NULL when "
             "alignment was not power of 2\n", tid);
      err = 1;
    }
  } /* end of parallel */
  return !err;
}
예제 #3
0
// Close a connection and free the memory associated and remove from hash
void closeConnection(ev_io *watcher) {
	ev_io_stop(libEvLoop, watcher); // Tell libev to stop following it
	close(watcher->fd); // Close the socket

	// Remove the client status from the hash if it's a waiting connection
	if (((clientStatus*)watcher)->readStatus==1000) { // Only ones waiting a message (1000) are in the hash
		khiter_t k = kh_get(clientStatuses, clientStatuses, ((clientStatus*)watcher)->clientId); // Find it in the hash
		if (k != kh_end(clientStatuses)) { // Was it in the hash?
			kh_del(clientStatuses, clientStatuses, k); // Remove it from the hash
		}
	}

	kmp_free(csPool, csPool, (clientStatus*)watcher); // Free the clientstatus/watcher (this is last because the fd is used above, after ev_io_stop)
}
예제 #4
0
// Close a connection and free the memory associated and skip removing from hash, only for use when a connection
// arrives and already was a message waiting for it
void closeConnectionSkipHash(ev_io *watcher) {
	ev_io_stop(libEvLoop, watcher); // Tell libev to stop following it
	close(watcher->fd); // Close the socket
	kmp_free(csPool, csPool, (clientStatus*)watcher); // Free the clientstatus/watcher (this is last because the fd is used above, after ev_io_stop)
}