tmp->base_addr = id.base;
tmp->len = id.bytes;
- tmp->max_page_id = tmp->len/BASE_PAGE_SIZE;
-
- // Datastructures for keeping track of buffers
- slab_init(&tmp->alloc, sizeof(struct buffer), slab_default_refill);
- slab_grow(&tmp->alloc, tmp->bufs, sizeof(struct buffer)*INIT_SIZE);
- tmp->used_bufs = calloc(1, sizeof(struct buffer*)*tmp->len/BASE_PAGE_SIZE);
*region = tmp;
*/
errval_t region_destroy(struct region* region)
{
- for (int i = 0; i < region->max_page_id; i++) {
- if (region->used_bufs[i] != NULL) {
- return DEVQ_ERR_REGION_DESTROY;
- }
- }
-
- free(region->used_bufs);
free(region);
return SYS_ERR_OK;
}
-/**
- * @brief Get a buffer id from a region
- *
- * @param region The region to get the buffer from
- * @param addr The physical address of the buffer
- * @param len lenght of the buffer
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-errval_t region_get_buffer_id(struct region* region,
- lpaddr_t addr,
- bufferid_t* buffer_id)
-{
- uint32_t page_id;
- *buffer_id = (addr - region->base_addr);
- page_id = (*buffer_id)/BASE_PAGE_SIZE;
-
- // Test if buffer can not be in region
- if (page_id > region->max_page_id) {
- return DEVQ_ERR_BUFFER_NOT_IN_REGION;
- }
-
- // Test if buffer is already used
- if (region_buffer_id_in_use(region, *buffer_id)) {
- return DEVQ_ERR_BUFFER_ALREADY_IN_USE;
- }
-
- struct buffer* tmp = slab_alloc(®ion->alloc);
- tmp->id = addr - region->base_addr;
- tmp->next = NULL;
- struct buffer* ele = region->used_bufs[page_id];
-
- // Empty list
- if (ele == NULL) {
- region->used_bufs[page_id] = tmp;
-
- DQI_DEBUG_REGION("buffer region=%d bucket=%d, id=%d, addr=%16lx\n",
- region->id, page_id, *buffer_id, addr);
- return SYS_ERR_OK;
- }
-
- // Iterate through list
- while (ele->next != NULL) {
- ele = ele->next;
- }
-
- ele->next = tmp;
-
- DQI_DEBUG_REGION("buffer region=%d bucket=%d, id=%d, addr=%16lx\n",
- region->id, page_id, *buffer_id, addr);
- return SYS_ERR_OK;
-}
-
-/**
- * @brief Set a certain buffer id as used
- *
- * @param region The region to get the buffer from
- * @param addr The physical address of the buffer
- * @param buffer_id The buffer id
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-errval_t region_set_buffer_id(struct region* region,
- lpaddr_t addr,
- bufferid_t buffer_id)
-{
- uint32_t page_id;
- page_id = buffer_id/BASE_PAGE_SIZE;
-
- // Test if buffer can not be in region
- if (page_id > region->max_page_id) {
- return DEVQ_ERR_BUFFER_NOT_IN_REGION;
- }
-
- // Test if buffer is already used
- if (region_buffer_id_in_use(region, buffer_id)) {
- return DEVQ_ERR_BUFFER_ALREADY_IN_USE;
- }
-
- struct buffer* tmp = slab_alloc(®ion->alloc);
- tmp->id = buffer_id;
- tmp->next = NULL;
- struct buffer* ele = region->used_bufs[page_id];
-
- // Empty list
- if (ele == NULL) {
- region->used_bufs[page_id] = tmp;
-
- DQI_DEBUG_REGION("buffer region=%d bucket=%d, id=%d, addr=%16lx\n",
- region->id, page_id, buffer_id, addr);
- return SYS_ERR_OK;
- }
-
- // Iterate through list
- while (ele->next != NULL) {
- ele = ele->next;
- }
-
- ele->next = tmp;
-
- DQI_DEBUG_REGION("buffer region=%d bucket=%d, id=%d, addr=%16lx\n",
- region->id, page_id, buffer_id, addr);
- return SYS_ERR_OK;
-}
-
-/**
- * @brief Return a buffer to the region
- *
- * @param region The region to return the buffer to
- * @param buffer_id The id of the buffer to return to the region
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-errval_t region_free_buffer_id(struct region* region,
- bufferid_t buffer_id)
-{
- uint32_t page_id;
- page_id = buffer_id/BASE_PAGE_SIZE;
- // Test if buffer can not be in region
- if (page_id > region->max_page_id) {
- return DEVQ_ERR_BUFFER_NOT_IN_REGION;
- }
-
- // Test if buffer is used
- if (!region_buffer_id_in_use(region, buffer_id)) {
- return DEVQ_ERR_BUFFER_NOT_IN_USE;
- }
-
- struct buffer* ele = region->used_bufs[page_id];
- // First entry is special case
- if (ele->id == buffer_id) {
- region->used_bufs[page_id] = ele->next;
- slab_free(®ion->alloc, ele);
- DQI_DEBUG_REGION("Returned buffer id=%d (first entry)\n", buffer_id);
- return SYS_ERR_OK;
- }
-
- while (ele != NULL) {
- if (ele->next->id == buffer_id) {
- struct buffer* ele_to_free = ele->next;
- ele->next = ele->next->next;
- slab_free(®ion->alloc, ele_to_free);
- DQI_DEBUG_REGION("Returned buffer id=%d (second or higher entry) \n",
- buffer_id);
- return SYS_ERR_OK;
- }
- ele = ele->next;
- }
-
- return SYS_ERR_OK;
-}
-
-
-/**
- * @brief Return a if a buffer to the region is in use
- *
- * @param region The region to return the buffer to
- * @param buffer_id The id of the buffer
- *
- * @returns true if the buffer is in use otherwise false
- */
-bool region_buffer_id_in_use(struct region* region,
- bufferid_t buffer_id)
-{
- uint32_t page_id;
- page_id = buffer_id/BASE_PAGE_SIZE;
-
- // Empty bucket -> can not be used
- if (region->used_bufs[page_id] == NULL) {
- return false;
- }
-
- // check list
- struct buffer* ele = region->used_bufs[page_id];
- while (ele != NULL) {
- if (ele->id == buffer_id) {
- return true;
- }
- ele = ele->next;
- }
-
- DQI_DEBUG_REGION("Returned buffer id=%d \n", buffer_id);
- return false;
-}
-
struct capref* cap;
// Lenght of the memory region
size_t len;
-
-
- // slab allocator for buffer structs
- struct slab_allocator alloc;
- // inital slab buffers
- struct buffer bufs[INIT_SIZE];
- // Data structure to keep track of buffers
- struct buffer** used_bufs;
- // Largest page id
- uint32_t max_page_id;
};
/**
*/
errval_t region_destroy(struct region* region);
-/**
- * @brief Get a buffer id from a region
- *
- * @param region The region to get the buffer from
- * @param addr The physical address of the buffer
- * @param buffer_id Return pointer to the buffer id of the buffer
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-errval_t region_get_buffer_id(struct region* region,
- lpaddr_t addr,
- bufferid_t* buffer_id);
-
-/**
- * @brief Set a certain buffer id as used
- *
- * @param region The region to get the buffer from
- * @param addr The physical address of the buffer
- * @param buffer_id The buffer id
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-errval_t region_set_buffer_id(struct region* region,
- lpaddr_t addr,
- bufferid_t buffer_id);
-
-/**
- * @brief Return a buffer id to the region
- *
- * @param region The region to return the buffer to
- * @param buffer_id The id of the buffer to return to the region
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-errval_t region_free_buffer_id(struct region* region,
- bufferid_t buffer_id);
-
-
-/**
- * @brief Return a if a buffer to the region is in use
- *
- * @param region The region to return the buffer to
- * @param buffer_id The id of the buffer
- *
- * @returns true if the buffer is in use otherwise false
- */
-bool region_buffer_id_in_use(struct region* region,
- bufferid_t buffer_id);
#endif /* REGION_H_ */
pool->num_regions++;
return SYS_ERR_OK;
}
+
/**
* @brief remove a memory region from the region pool
*
*
* @returns error on failure or SYS_ERR_OK on success
*/
+/*
static errval_t region_pool_get_region(struct region_pool* pool,
regionid_t region_id,
struct region** region)
return SYS_ERR_OK;
}
-
-
-/**
- * @brief get a buffer id from a region
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region to get the buffer from
- * @param addr The physical address of the buffer
- * @param buffer_id Return pointer to the buffer id
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-
-errval_t region_pool_get_buffer_id_from_region(struct region_pool* pool,
- regionid_t region_id,
- lpaddr_t addr,
- bufferid_t* buffer_id)
-{
- errval_t err;
- struct region* region;
- err = region_pool_get_region(pool, region_id, ®ion);
- if (err_is_fail(err)) {
- return err;
- }
-
- err = region_get_buffer_id(region, addr, buffer_id);
- if (err_is_fail(err)) {
- return err;
- }
-
- return SYS_ERR_OK;
-}
-
-
-/**
- * @brief Set a buffer of a region as used
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region to set the buffer as used
- * @param addr The physical address of the buffer
- * @param buffer_id The buffer id
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-
-errval_t region_pool_set_buffer_id_from_region(struct region_pool* pool,
- regionid_t region_id,
- lpaddr_t addr,
- bufferid_t buffer_id)
-{
- errval_t err;
- struct region* region;
- err = region_pool_get_region(pool, region_id, ®ion);
- if (err_is_fail(err)) {
- return err;
- }
-
- err = region_set_buffer_id(region, addr, buffer_id);
- if (err_is_fail(err)) {
- printf("%s", err_getstring(err));
- return err;
- }
-
- return SYS_ERR_OK;
-}
-
-/**
- * @brief returns the buffer id to the pool of free ids
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region to get the buffer from
- * @param buffer_id Return pointer to the buffer id
- * @param addr the physical address of the buffer
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-
-errval_t region_pool_return_buffer_id_to_region(struct region_pool* pool,
- regionid_t region_id,
- bufferid_t buffer_id)
-{
- errval_t err;
- struct region* region;
- err = region_pool_get_region(pool, region_id, ®ion);
- if (err_is_fail(err)) {
- return err;
- }
-
- err = region_free_buffer_id(region, buffer_id);
- if (err_is_fail(err)) {
- return err;
- }
-
- return SYS_ERR_OK;
-}
-
-
-/**
- * @brief return a buffer to a region of the pool
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region to return the buffer to
- * @param addr Address of the buffer
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-
-errval_t region_pool_return_buffer_to_region(struct region_pool* pool,
- regionid_t region_id,
- lpaddr_t addr)
-{
- errval_t err;
- struct region* region;
- err = region_pool_get_region(pool, region_id, ®ion);
- if (err_is_fail(err)) {
- return err;
- }
-
- lpaddr_t base = region->base_addr;
- bufferid_t bid = addr - base;
-
- err = region_free_buffer_id(region, bid);
- if (err_is_fail(err)) {
- return err;
- }
-
- return SYS_ERR_OK;
-}
-/**
- * @brief return if a buffer of a region is in use
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region
- * @param buffer_id The id of the buffer
- *
- * @returns true if the buffer is in use otherwise false
- */
-bool region_pool_buffer_id_of_region_in_use(struct region_pool* pool,
- regionid_t region_id,
- bufferid_t buffer_id)
-{
- errval_t err;
- struct region* region;
- err = region_pool_get_region(pool, region_id, ®ion);
- if (err_is_fail(err)) {
- return false;
- }
-
- return region_buffer_id_in_use(region, buffer_id);
-}
-
+*/
/**
* @brief check if buffer is valid
struct capref* cap);
/**
- * @brief get a page sized buffer from a region of the pool
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region to get the buffer from
- * @param addr The physical address of the buffer
- * @param buffer_id Return pointer to the buffer id
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-
-errval_t region_pool_get_buffer_id_from_region(struct region_pool* pool,
- bufferid_t region_id,
- lpaddr_t addr,
- bufferid_t* buffer_id);
-
-/**
- * @brief Set a buffer of a region as used
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region to set the buffer as used
- * @param addr The physical address of the buffer
- * @param buffer_id The buffer id
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-
-errval_t region_pool_set_buffer_id_from_region(struct region_pool* pool,
- regionid_t region_id,
- lpaddr_t addr,
- bufferid_t buffer_id);
-/**
- * @brief return a buffer to a region of the pool
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region to return the buffer to
- * @param buffer_id The id of the buffer to return to the region
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-
-errval_t region_pool_return_buffer_id_to_region(struct region_pool* pool,
- regionid_t region_id,
- bufferid_t buffer_id);
-/**
- * @brief return a buffer to a region of the pool
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region to return the buffer to
- * @param addr Address of the buffer
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-
-errval_t region_pool_return_buffer_to_region(struct region_pool* pool,
- regionid_t region_id,
- lpaddr_t addr);
-/**
- * @brief return if a buffer of a region is in use
- *
- * @param pool The pool to get the region from
- * @param region_id The id of the region
- * @param buffer_id The id of the buffer
- *
- * @returns error on failure or SYS_ERR_OK on success
- */
-bool region_pool_buffer_id_of_region_in_use(struct region_pool* pool,
- regionid_t region_id,
- bufferid_t buffer_id);
-
-/**
* @brief check if buffer is valid
*
* @param pool The pool to get the region from