Example #1
0
void * ring_poolmanager_allocate ( RingState *pRingState,size_t size )
{
	void *pMemory  ;
	pMemory = NULL ;
	/* If No memory - Create new block */
	if ( (pRingState->vPoolManager.pCurrentItem == NULL) && (pRingState->vPoolManager.pBlockStart == NULL)  && (pRingState->lStartPoolManager) ) {
		ring_poolmanager_newblock(pRingState);
	}
	/* Get Item from the Pool Manager */
	if ( pRingState->vPoolManager.pCurrentItem != NULL ) {
		pMemory = pRingState->vPoolManager.pCurrentItem ;
		pRingState->vPoolManager.pCurrentItem = pRingState->vPoolManager.pCurrentItem->pNext ;
	}
	/* If no free items, Allocate new item */
	else {
		pMemory = ring_malloc(size);
		/* Check Memory */
		if ( pMemory == NULL ) {
			printf( RING_OOM ) ;
			exit(0);
		}
	}
	#if RING_TRACKALLOCATIONS
	pRingState->vPoolManager.nSmallAllocCount++ ;
	#endif
	return pMemory ;
}
Example #2
0
static PyObject*
init(PyObject *self, PyObject *args)
{
  ring = ring_malloc(RING_SIZE);
  init_serialize(ring);
  return Py_BuildValue("");
}
Example #3
0
RING_API void * ring_state_malloc ( void *pState,size_t size )
{
	#if RING_USEPOOLMANAGER
	if ( pState != NULL ) {
		#if RING_TRACKALLOCATIONS
		((RingState *) pState)->vPoolManager.nAllocCount++ ;
		#endif
		if ( size <= RING_POOLMANAGER_ITEMSIZE ) {
			return ring_poolmanager_allocate((RingState *) pState,size) ;
		}
	}
	#endif
	return ring_malloc(size) ;
}
Example #4
0
/** Register a function to be called after N_TICKS have elapsed from now.
 * PERIOIDIC_P is nonzero if the timer function should be called repeatedly,
 * every time that much time has elapsed.
 * FN is the function to be called and DATA can be anything at all, passed to
 * the handler. */
void sim_time_register (int n_ticks, int periodic_p, time_handler_t fn, void *data)
{
	unsigned int ring = ring_later (n_ticks);

	struct time_handler *elem = ring_malloc ();
	if (!elem)
		simlog (SLC_DEBUG, "can't alloc ring");

	if (n_ticks > RING_COUNT)
		simlog (SLC_DEBUG, "can't schedule timer that far out");

	elem->next = time_handler_ring[ring];
	elem->periodicity = periodic_p ? n_ticks : 0;
	elem->fn = fn;
	elem->data = data;
	time_handler_ring[ring] = elem;
}
Example #5
0
void test_ring() {
  int size;
  unsigned char *buf = malloc(sizeof(unsigned char) * BUF_SIZE);
  Ring *ring = ring_malloc(BUF_SIZE);
  RingReader *reader = reader_malloc(ring);

  size = reader_read(reader, buf);
  assert(0 == size);  
  
  printf("one write and one read\n");
  ring_write(ring, "11", 2);
  size = reader_read(reader, buf);
  assert(2 == size);
  assert(0 == memcmp(buf, "11", 2));

  printf("two writes and one read due to overflow\n");
  ring_write(ring, "22", 2);
  ring_write(ring, "33", 2);
  size = reader_read(reader, buf);
  assert(-1 == size);
  size = reader_read(reader, buf);
  assert(2 == size);
  assert(0 == memcmp(buf, "33", 2));
  
  printf("two small writes and two reads\n");
  ring_write(ring, "4", 1);
  ring_write(ring, "5", 1);
  size = reader_read(reader, buf);
  assert(1 == size);
  assert(0 == memcmp(buf, "4", 1));
  size = reader_read(reader, buf);
  assert(1 == size);
  assert(0 == memcmp(buf, "5", 1));  

  printf("fill once again\n");
  ring_write(ring, "123456", 6);
  size = reader_read(reader, buf);
  assert(6 == size);
  assert(0 == memcmp(buf, "123456", 6));

  ring_free(ring);
}
Example #6
0
void test_raw() {
  Ring *ring = ring_malloc(BUF_SIZE);
  unsigned char *buf = malloc(sizeof(unsigned char) * BUF_SIZE);
  unsigned char *reference = malloc(sizeof(unsigned char) * BUF_SIZE);
  int i;
  for (i=0; i < BUF_SIZE; i++) {
    reference[i] = i;
  }

  // raw write entire buffer and verify
  ring_raw_write(ring, 0, reference, BUF_SIZE);
  ring_raw_read(ring, buf, 0, BUF_SIZE);
  assert(0 == memcmp(reference, buf, BUF_SIZE));

  // raw write entire buffer from middle and verify
  ring_raw_write(ring, BUF_SIZE / 2, reference, BUF_SIZE);
  ring_raw_read(ring, buf, BUF_SIZE / 2, BUF_SIZE);
  assert(0 == memcmp(reference, buf, BUF_SIZE));
  
  ring_free(ring);
}