Ejemplo n.º 1
0
	/**	@fn	char CBufferLoop::get_buffer_tmp(char* pbuffer, int nbuffer_size, int* nreal_buffer_size)
	*	@brief 获取指定大小的数据,但是不会清空该数据
	*	@param[in] pbuffer 数据存放指针
	*	@param[in] nbuffer_size 想要获取的大小
	*	@param[in] nreal_buffer_size 实际获取的大小
	*	@return	
	*/
	char get_buffer_tmp(char* pbuffer, int nbuffer_size, int* nreal_buffer_size)
	{
		bool bret = true;
		int nrealsize = (nbuffer_size < get_used_size() ? nbuffer_size : get_used_size());
		if (m_nhead_pos <= m_ntail_pos)
		{
			memcpy(pbuffer, &m_pbuffer[m_nhead_pos], nrealsize);
		}
		else
		{
			int nrestsize = m_nbuffer_size - m_nhead_pos;
			//到末尾的长度大于需要的长度
			if (nrealsize <= nrestsize)
			{
				memcpy(pbuffer, &m_pbuffer[m_nhead_pos], nrealsize);
			}
			else
			{
				//需要分两次获取,先把到末尾的数据全部获取
				memcpy(pbuffer, &m_pbuffer[m_nhead_pos], nrestsize);
				//然后从起始处获取剩余的数据
				memcpy(&pbuffer[nrestsize], &m_pbuffer[0], nrealsize - nrestsize);
			}
		}
		*nreal_buffer_size = nrealsize;
		return bret;
	}
Ejemplo n.º 2
0
static void interface_free_func(void** state) {
	void* malloc_pool = malloc(POOL_SIZE);
	init_memory_pool(POOL_SIZE, malloc_pool, 0);

	size_t first_size = get_used_size(malloc_pool);

	IPv4Interface* interface = interface_alloc(malloc_pool);

	size_t comp_size = get_used_size(malloc_pool);
	assert_int_not_equal(comp_size, first_size);

	interface_free(interface, malloc_pool);

	comp_size = get_used_size(malloc_pool);
	assert_int_equal(comp_size, first_size);

	destroy_memory_pool(malloc_pool);
	free(malloc_pool);
	malloc_pool = NULL;
}
Ejemplo n.º 3
0
void event_write(unsigned long index, unsigned long len)
{
	if (page_table[index].addr)
		free_ex(page_table[index].addr, pool);

	page_table[index].addr = malloc_ex(len, pool);
	if (page_table[index].addr == NULL) {
		printf("Error allocating for index=%lu, len=%lu\n", index, len);
		return;
	}
	page_table[index].len = len;
	printf("%u\n", KB(get_used_size(pool)));
}
Ejemplo n.º 4
0
//write data into interface
void write_to_interface(char * buffer, int buffer_size)
{
	if (!is_device_ready())
	{
		return;
	}
	
	if (0 == buffer_size)
	{
		return;
	}

	PVERBOSE("write %d byte(s) buffer to cache now\n", buffer_size);
	
	if (down_interruptible(&sem))
	{
		return;
	}

	//we will never let our buffer be full
	//note when cycle_buffer_size - get_used_size() = buffer_size, all buffer will be filled, then read_ptr = write_ptr
	//but we have no idea buffer size is 0 or full when read_ptr = write_ptr, so avoid this states
	if ((cycle_buffer_size - get_used_size()) > buffer_size)
	{
		//buffer is enough
		write_ptr = copy_to_buffer(write_ptr, buffer, buffer_size);
		
		PVERBOSE("write complete, read_ptr: 0x%08x, write_ptr: 0x%08x, used size: %d\n", read_ptr, write_ptr, get_used_size());
	}
	else
	{
		PWARN("failed while write to interface, buffer is full, used size: %d, need: %d\n", get_used_size(), buffer_size);
	}
	
	up(&sem);
}
Ejemplo n.º 5
0
U32 nx_memalloc_used(void) {
  return get_used_size(mp);
}
Ejemplo n.º 6
0
//read sys_call will invoke this, do read something
ssize_t interface_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
	int used = 0;
	int min = 0;
	int left = 0;
	int copyed = 0;
	int first_size = 0;
	
	if (down_interruptible(&sem))
	{
		copyed = -EINTR;
		
		goto cleanup;
	}
	
	PVERBOSE("read begin, read_ptr: 0x%08x, write_ptr: 0x%08x, used size: %d, want to read %d byte(s)\n", read_ptr, write_ptr, get_used_size(), count);
	
	used = get_used_size();
	if (count > used)
	{
		min = used;
	}
	else
	{
		min = count;
	}	

	first_size = cycle_buffer_size - (read_ptr - cycle_buffer);
	
	if (read_ptr == write_ptr)
	{
		copyed = 0;
		
		goto cleanup;
	}
	else if (read_ptr > write_ptr && first_size <= min)
	{
		//deal with read cross boundary
		int first_copy = 0;
		int left = 0;

		ASSERT(first_size > 0);

		PDEBUG("read cross boundary, left %d byte(s), need %d byte(s)\n", first_size, min);

		//read buffer is bigger than this part, so read all left out 
		first_copy = first_size;
			
		left = copy_to_user(buf, read_ptr, first_copy);
		if (0 == left)
		{
			copyed = first_copy;
		}
		else
		{
			//copy not completed, go cleanup
			copyed = first_copy - left;

			PWARN("copy_to_user not completed (%d, %d) inside interface_read\n", left, first_copy);
			
			goto cleanup;
		}
			
		read_ptr = cycle_buffer;
		buf += copyed;
		min -= copyed;
	}

	if (min > 0) 
	{
		//read second part or read normally
		PVERBOSE("read secondly or normally %d byte(s) at 0x%08x\n", min, read_ptr);
		
		left = copy_to_user(buf, read_ptr, min);
		
		if (0 == left)
		{
			copyed += min;
			read_ptr += min;
		}
		else
		{
			copyed += min - left;
			read_ptr += min - left;

			PWARN("copy_to_user not completed (%d, %d) inside interface_read\n", left, min);
		}
	}
	
cleanup:
	up(&sem);
	
	PVERBOSE("read complete, read_ptr: 0x%08x, write_ptr: 0x%08x, used size: %d, copyed: %d\n", read_ptr, write_ptr, get_used_size(), copyed);
	
	return copyed;	
}
Ejemplo n.º 7
0
	int get_rest_size() { return get_buffer_size() - get_used_size(); }
Ejemplo n.º 8
0
/**	@fn	int CBufferLoop::get_rest_size()
*	@brief 
*	@return	
*/
int CBufferLoop::get_rest_size()
{
	return get_buffer_size() - get_used_size();
}
Ejemplo n.º 9
0
size_t malloc_used() {
	return get_used_size(__malloc_pool);
}