示例#1
0
文件: list.c 项目: rgs1/libsmall
SMALL_EXPORT void list_resize(list *l, int new_size)
{
  assert(new_size > l->size);

  pool_resize(l->pool, sizeof(list_item) * new_size);
  l->size = new_size;
}
示例#2
0
文件: test-pool.c 项目: rgs1/libsmall
static void test_resize(void)
{
  void *a, *b, *c;
  pool *p = pool_new(20, 10);

  a = pool_get(p);
  assert(a);

  b = pool_get(p);
  assert(b);

  assert(pool_get(p) == NULL);

  pool_resize(p, 30);

  c = pool_get(p);
  assert(c);

  assert(pool_get(p) == NULL);

  /* put them back */
  pool_put(p, a);
  pool_put(p, b);
  pool_put(p, c);

  /* get them again */
  a = pool_get(p);
  assert(a);

  b = pool_get(p);
  assert(b);

  c = pool_get(p);
  assert(c);

  assert(pool_get(p) == NULL);

  /* put them back, again */
  pool_put(p, a);
  pool_put(p, b);
  pool_put(p, c);

  pool_destroy(p);
}
示例#3
0
void mqueue_start()
{
	char message_buffer[MQUEUE_BUFFER_SIZE];
	
	from_client = mq_open(MQUEUE_SERVER, O_RDONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR, NULL);
	to_client = mq_open(MQUEUE_CLIENT, O_WRONLY | O_EXCL | O_CREAT, S_IWUSR | S_IRUSR, NULL);
	
	if(from_client == -1 || to_client == -1)
	{
	  log_error("Failed to open administration interface, mq_open failed");
	  master_stop();
	}
	
	log_info("Apricot admin interface opened");
	  
	for(;;)
	{
		/* receive a message from the client administration interface */
		if(mq_receive(from_client, message_buffer, MQUEUE_BUFFER_SIZE, NULL) == -1)
		{
		  log_error("mq_receive failed : %s", strerror(errno));
		  continue;
		}
		
		/* process interface request */
		
		if(!strcasecmp(message_buffer, MQUEUE_SEND_LOG))
		{
			FILE * log_file = log_file = fopen(conf.log_file, "r");
			
			if(!log_file)
			{
			  strcpy(message_buffer, MQUEUE_FAIL);
			  send(to_client, message_buffer);
			}
			else
			{
			  while(fgets(message_buffer, MQUEUE_BUFFER_SIZE-1, log_file))
			  {
				send(to_client, message_buffer);
			  }
			  
			  fclose(log_file);
			  strcpy(message_buffer, MQUEUE_OK);
			  send(to_client, message_buffer);
			}
		}
		else if(!strcasecmp(message_buffer, MQUEUE_TRUNCATE_LOG))
		{
			/* to preserve log consistency */
			log_lock();
			truncate(conf.log_file, 0);
			log_unlock();
			
			strcpy(message_buffer, MQUEUE_OK);
			send(to_client, message_buffer);
		}
		else if(!strcasecmp(message_buffer, MQUEUE_STOP_SERVER))
		{	
			log_info("Apricot stop asked by admin interface");
			master_stop();
			exit(EXIT_SUCCESS);
		}
		else if(!strcasecmp(message_buffer, MQUEUE_RESIZE_POOL))
		{
			log_info("Pool resize asked by admin interface");
			
			/* receive a message from the client administration interface */
			if(mq_receive(from_client, message_buffer, MQUEUE_BUFFER_SIZE, NULL) == -1)
			{
			  log_error("mq_receive failed to receive new pool size : %s", strerror(errno));
			  continue;
			}
			
			int new_size;
			
			if(sscanf(message_buffer, "%i", &new_size) != 1 || new_size <= 0)
			{
				log_error("invalid pool size %i received from admin interface", new_size);
				strcpy(message_buffer, MQUEUE_FAIL);
				send(to_client, message_buffer);
				continue;
			}
			
			pool_resize(new_size);
			
			strcpy(message_buffer, MQUEUE_OK);
			send(to_client, message_buffer);
		}
		else
		{
		   	strcpy(message_buffer, MQUEUE_FAIL);
		   	send(to_client, message_buffer);
		}
	}
}