OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [libmem/] [tests/] [test_memcache.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Testing code for the memcache structure.
3
 *
4
 * Copyright (C) 2007 Bahadir Balban
5
 */
6
#include <time.h>
7
#include <stdlib.h>
8
#include <stdio.h>
9
#include <string.h>
10
#include <memcache/memcache.h>
11
#include "test_memcache.h"
12
#include "test_alloc_generic.h"
13
#include "debug.h"
14
#include "tests.h"
15
 
16
#include <l4/macros.h>
17
#include <l4/config.h>
18
#include <l4/types.h>
19
#include <l4/lib/list.h>
20
#include INC_GLUE(memory.h)
21
 
22
unsigned int MEM_CACHE_SIZE;
23
 
24
struct mem_cache *this;
25
 
26
void *buffer;
27
 
28
void *mem_cache_alloc_wrapped(int size)
29
{
30
        return mem_cache_alloc(this);
31
}
32
 
33
int mem_cache_free_wrapped(void *addr)
34
{
35
        return mem_cache_free(this, addr);
36
}
37
 
38
void print_memcache_state(void)
39
{
40
        printf("%-15s%d\n","Total:", this->total);
41
        printf("%-15s%d\n","Free:", this->free);
42
        printf("Bitmap has %d words:\n", BITWISE_GETWORD(this->total) + 1);
43
        for (int i = 0; i <= BITWISE_GETWORD(this->total); i++)
44
                printf("0x%x\n", this->bitmap[i]);
45
}
46
 
47
int test_memcache_init_aligned(int *items_max, int item_size)
48
{
49
        if (item_size * 10 > MEM_CACHE_SIZE)
50
                MEM_CACHE_SIZE = item_size * 10;
51
        if (!(buffer = calloc(1, MEM_CACHE_SIZE))) {
52
                printf("System out of memory.\n");
53
                BUG();
54
        }
55
        if ((this = mem_cache_init(buffer, MEM_CACHE_SIZE,
56
                                   item_size, 1)) == 0) {
57
                printf("Unable to initialise cache.\n");
58
                return -1;
59
        }
60
        *items_max = mem_cache_total_empty(this);
61
        printf("\nMEMCACHE TEST: ALIGNED ELEMENTS\n==========================\n");
62
        printf("%-30s%d\n", "Item size:", item_size);
63
        printf("%-30s0x%x\n", "Cache occupied space:", MEM_CACHE_SIZE);
64
        printf("%-30s%d\n","Total items in cache:", *items_max);
65
        printf("%-30s0x%x\n","Total items space:", (*items_max * item_size));
66
        return 0;
67
}
68
 
69
 
70
int test_memcache_init(int *items_max, int item_size)
71
{
72
        if (item_size * 10 > MEM_CACHE_SIZE)
73
                MEM_CACHE_SIZE = item_size * 10;
74
        printf("%s: Allocating cache memory.\n",__FUNCTION__);
75
        if (!(buffer = calloc(1, MEM_CACHE_SIZE))) {
76
                printf("System out of memory.\n");
77
                BUG();
78
        }
79
        if ((this = mem_cache_init(buffer, MEM_CACHE_SIZE,
80
                                   item_size, 0)) == 0) {
81
                printf("Unable to initialise cache.\n");
82
                return -1;
83
        }
84
        *items_max = mem_cache_total_empty(this);
85
        printf("\nMEMCACHE TEST:\n========================\n");
86
        printf("%-30s%d\n", "Item size:", item_size);
87
        printf("%-30s0x%x\n", "Cache occupied space:", MEM_CACHE_SIZE);
88
        printf("%-30s%d\n","Total items in cache:", *items_max);
89
        printf("%-30s0x%x\n","Total items space:", (*items_max * item_size));
90
        return 0;
91
}
92
 
93
int test_memcache(int items_max, int item_size, FILE *init_state, FILE *exit_state, int aligned)
94
{
95
        const unsigned int TEST_CACHE_ITEM_SIZE = 5;
96
        MEM_CACHE_SIZE = PAGE_SIZE * 5;
97
        if (!item_size)
98
                item_size = TEST_CACHE_ITEM_SIZE;
99
        /* items_max value is ignored and overwritten because caches have fixed size. */
100
        test_memcache_init(&items_max, item_size);
101
        test_alloc_free_random_order(items_max, /* unused */ 2, mem_cache_alloc_wrapped,
102
                                     mem_cache_free_wrapped, print_memcache_state,
103
                                     init_state, exit_state);
104
        free(buffer);
105
        if (aligned) {
106
                test_memcache_init_aligned(&items_max, item_size);
107
                test_alloc_free_random_order(items_max, /* unused */ 2, mem_cache_alloc_wrapped,
108
                                             mem_cache_free_wrapped, print_memcache_state,
109
                                             init_state, exit_state);
110
        }
111
        free(buffer);
112
        return 0;
113
}
114
 
115
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.