1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// memfix2.cxx
|
4 |
|
|
//
|
5 |
|
|
// Fixed memory pool test 2
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): dsm, jlarmour
|
43 |
|
|
// Contributors:
|
44 |
|
|
// Date: 2000-06-18
|
45 |
|
|
// Description: test allocation and freeing in fixed memory pools
|
46 |
|
|
//####DESCRIPTIONEND####
|
47 |
|
|
|
48 |
|
|
#include <pkgconf/memalloc.h>
|
49 |
|
|
#include <pkgconf/system.h>
|
50 |
|
|
|
51 |
|
|
#ifdef CYGPKG_KERNEL
|
52 |
|
|
#include <pkgconf/kernel.h>
|
53 |
|
|
|
54 |
|
|
#include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start()
|
55 |
|
|
#include <cyg/kernel/thread.hxx> // Cyg_Thread
|
56 |
|
|
#include <cyg/kernel/thread.inl>
|
57 |
|
|
#include <cyg/kernel/sema.hxx>
|
58 |
|
|
|
59 |
|
|
#include <cyg/kernel/sched.inl>
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
#define NTHREADS 1
|
63 |
|
|
#include "testaux.hxx"
|
64 |
|
|
|
65 |
|
|
#endif
|
66 |
|
|
|
67 |
|
|
#include <cyg/memalloc/memfixed.hxx>
|
68 |
|
|
|
69 |
|
|
#include <cyg/infra/testcase.h>
|
70 |
|
|
|
71 |
|
|
static const cyg_int32 memsize = 1024;
|
72 |
|
|
|
73 |
|
|
static cyg_uint8 mem[memsize];
|
74 |
|
|
|
75 |
|
|
#define NUM_PTRS 16 // Should be even
|
76 |
|
|
#define BLOCKSIZE 12
|
77 |
|
|
|
78 |
|
|
static Cyg_Mempool_Fixed mempool(mem, memsize, BLOCKSIZE);
|
79 |
|
|
|
80 |
|
|
static cyg_uint8 *ptr[NUM_PTRS];
|
81 |
|
|
|
82 |
|
|
// We make a number of passes over a table of pointers which point to
|
83 |
|
|
// blocks of allocated memory. The block is freed and a new block
|
84 |
|
|
// allocated. The order of the processing of blocks is varied.
|
85 |
|
|
static void entry( CYG_ADDRWORD data )
|
86 |
|
|
{
|
87 |
|
|
for(cyg_ucount32 passes = 0; passes < 10; passes++) {
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
// The order which the table is processed varies according to
|
91 |
|
|
// stepsize.
|
92 |
|
|
cyg_ucount8 stepsize = (passes*2 + 1) % NUM_PTRS; // odd
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
for(cyg_ucount8 c=0, i=0; c < NUM_PTRS; c++) {
|
96 |
|
|
i = (i+stepsize) % NUM_PTRS;
|
97 |
|
|
if(ptr[i]) {
|
98 |
|
|
for(cyg_ucount32 j=BLOCKSIZE;j--;) {
|
99 |
|
|
CYG_TEST_CHECK(ptr[i][j]==i, "Memory corrupted");
|
100 |
|
|
}
|
101 |
|
|
CYG_TEST_CHECK(mempool.free(ptr[i]), "bad free");
|
102 |
|
|
}
|
103 |
|
|
ptr[i] = mempool.try_alloc();
|
104 |
|
|
|
105 |
|
|
CYG_TEST_CHECK(NULL != ptr[i], "Memory pool not big enough");
|
106 |
|
|
CYG_TEST_CHECK(mem<=ptr[i] && ptr[i]+BLOCKSIZE < mem+memsize,
|
107 |
|
|
"Allocated region not within pool");
|
108 |
|
|
|
109 |
|
|
// Scribble over memory to check whether region overlaps
|
110 |
|
|
// with other regions. The contents of the memory are
|
111 |
|
|
// checked on freeing. This also tests that the memory
|
112 |
|
|
// does not overlap with allocator memory structures.
|
113 |
|
|
for(cyg_ucount32 j=BLOCKSIZE;j--;) {
|
114 |
|
|
ptr[i][j]=i;
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
CYG_TEST_PASS_FINISH("Fixed memory pool 2 OK");
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
void memfix2_main( void )
|
124 |
|
|
{
|
125 |
|
|
CYG_TEST_INIT();
|
126 |
|
|
CYG_TEST_INFO("Starting Fixed memory pool 2 test");
|
127 |
|
|
|
128 |
|
|
for(cyg_ucount32 i = 0; i<NUM_PTRS; i++) {
|
129 |
|
|
ptr[i] = NULL;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
#ifdef CYGPKG_KERNEL
|
133 |
|
|
new_thread(entry, 0);
|
134 |
|
|
Cyg_Scheduler::start();
|
135 |
|
|
#else
|
136 |
|
|
entry(0);
|
137 |
|
|
#endif
|
138 |
|
|
|
139 |
|
|
CYG_TEST_FAIL_FINISH("Not reached");
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
externC void
|
143 |
|
|
cyg_start( void )
|
144 |
|
|
{
|
145 |
|
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
146 |
|
|
cyg_hal_invoke_constructors();
|
147 |
|
|
#endif
|
148 |
|
|
memfix2_main();
|
149 |
|
|
}
|
150 |
|
|
// EOF memfix2.cxx
|