xref: /nrf52832-nimble/rt-thread/components/dfs/filesystems/uffs/src/uffs/uffs_mem.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2   This file is part of UFFS, the Ultra-low-cost Flash File System.
3 
4   Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
5 
6   UFFS is free software; you can redistribute it and/or modify it under
7   the GNU Library General Public License as published by the Free Software
8   Foundation; either version 2 of the License, or (at your option) any
9   later version.
10 
11   UFFS is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   or GNU Library General Public License, as applicable, for more details.
15 
16   You should have received a copy of the GNU General Public License
17   and GNU Library General Public License along with UFFS; if not, write
18   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19   Boston, MA  02110-1301, USA.
20 
21   As a special exception, if other files instantiate templates or use
22   macros or inline functions from this file, or you compile this file
23   and link it with other works to produce a work based on this file,
24   this file does not by itself cause the resulting work to be covered
25   by the GNU General Public License. However the source code for this
26   file must still be made available in accordance with section (3) of
27   the GNU General Public License v2.
28 
29   This exception does not invalidate any other reasons why a work based
30   on this file might be covered by the GNU General Public License.
31 */
32 
33 /**
34  * \file uffs_mem.c
35  * \brief uffs static memory allocator
36  * \author Ricky Zheng, created 23th Feb, 2007
37  */
38 
39 #include <string.h>
40 #include "uffs_config.h"
41 #include "uffs/uffs_types.h"
42 #include "uffs/uffs_public.h"
43 #include "uffs/uffs_os.h"
44 #include "uffs/uffs_mem.h"
45 
46 #define PFX "mem : "
47 
48 #if CONFIG_USE_STATIC_MEMORY_ALLOCATOR > 0
static_malloc(struct uffs_DeviceSt * dev,unsigned int size)49 static void * static_malloc(struct uffs_DeviceSt *dev, unsigned int size)
50 {
51 	struct uffs_memAllocatorSt *mem = &dev->mem;
52 	void *p = NULL;
53 
54 	size += (size % sizeof(long) ? sizeof(long) - (size % sizeof(long)) : 0);
55 
56 	if (mem->buf_size - mem->pos < (int)size) {
57 		uffs_Perror(UFFS_MSG_SERIOUS,
58 					"Memory alloc failed! (alloc %d, free %d)",
59 					size, mem->buf_size - mem->pos);
60 	}
61 	else {
62 		p = mem->buf_start + mem->pos;
63 		mem->pos += size;
64 		uffs_Perror(UFFS_MSG_NOISY,
65 					"0x%p: Allocated %d, free %d",
66 					p, size, mem->buf_size - mem->pos);
67 	}
68 
69 	return p;
70 }
71 
uffs_MemSetupStaticAllocator(uffs_MemAllocator * allocator,void * pool,int size)72 void uffs_MemSetupStaticAllocator(uffs_MemAllocator *allocator,
73 								  void *pool, int size)
74 {
75 	allocator->buf_start = (char *)pool;
76 	allocator->buf_size = size;
77 	allocator->pos = 0;
78 	allocator->malloc = static_malloc;
79 	allocator->free = NULL;  //never free memory for static memory allocator
80 
81 	uffs_Perror(UFFS_MSG_NOISY,
82 				"System static memory: %d bytes", allocator->buf_size);
83 
84 }
85 
86 #endif
87 
88 
89 
90 
91 
92 
93