Single chip microcomputer simple memory manager
This code is based on the non-operating system of STM32 single-chip development, powerful, can apply to address space of different size of memory space, and user interface is simple, easy to use
Reprint Please specify source: http://blog.csdn.net/u011833609/article/details/46834203
Memory.h
#ifndef __memory_h__#define __memory_h__#include "stdio.h" #include "string.h" #include "includes.h"//user use typedef struct{ void *addr; The starting address of the application to the memory uint32_t size; The size of the requested memory, allocated according to block size, is greater than or equal to the requested size uint16_t TB; Application Form serial number, application memory allocation, free memory use, the user does not use}dmem;//if return empty, the application fails DMEM *dynmemget (uint32_t size); void Dynmemput (DMEM *pdmem); #endif// __memory_h__
Memory.c
#include "memory.h" #define DMEM_BLOCK_SIZE 256//memory block size is 128 bytes # define Dmem_block_ NUM 20//memory block number is 40 # # dmem_total_size (dmem_block_size*dmem_block_num)///Memory Total size typedef enum{ Dmem_free = 0, dmem_used = 1,}dmem_used_item;typedef struct{Dmem_used_item used; Use state uint16_t blk_s; Starting block ordinal uint16_t blk_num; Number of blocks}dmem_apply;typedef struct{Dmem_used_item Tb_blk[dmem_block_num]; DMEM Tb_user[dmem_block_num]; User request memory information dmem_apply Tb_apply[dmem_block_num]; The system allocates memory information uint16_t Apply_num; Memory requisition table occupies a number of uint16_t blk_num; Memory block occupy number}dmem_state;static uint8_t dmemory[dmem_total_size];static dmem_state dmems = {0};
DMEM *dynmemget (uint32_t size) {uint16_t loop = 0; uint16_t find = 0; uint16_t blk_num_want = 0; DMEM * user = NULL; Dmem_apply *apply = NULL; The requested memory size cannot be 0 if (size = = 0) {return NULL; }//application memory cannot exceed total memory size if (Size > Dmem_total_size) {return NULL; }//application memory cannot exceed the remaining memory size if (Size > (dmem_block_num-dmems.blk_num) * dmem_block_size) {return NULL; }//The application form must have spare if (Dmems.apply_num >= dmem_block_num) {return NULL; }//Calculate the number of contiguous blocks required Blk_num_want = (size + dmem_block_size-1)/dmem_block_size; Look for the application form for (loop = 0; loop < Dmem_block_num; loop++) {if (dmems.tb_apply[loop].used = = Dmem_free) { Apply = &DMEMS.tb_apply[loop]; Application form found user = &DMEMS.tb_user[loop]; User table corresponding to find USER->TB = loop; Application Form number Record user->size = Blk_num_want * dmem_block_size; Allocation of largeSmall calculation break; }}//did not find the available application form, in theory it does not occur, the application of the remainder has been checked on the above if (loop = = Dmem_block_num) {return NULL; }//Search for contiguous memory block for (loop = 0; loop < Dmem_block_num; loop++) {if (dmems.tb_blk[loop] = = Dmem_free) {//Find the first free block of memory for (find = 1; (Find < Blk_num_want) && (loop + Find < dmem_block_num); Find + +) {//finds the next free memory block if (Dmems.tb_blk[loop + find]! = Dmem_free) {//Discover used memory block Break }} if (find >= blk_num_want) {//The number of free memory blocks found is sufficient user->addr = Dmemory + Loop * dmem_block_size; Calculates the address of the requested memory apply->blk_s = loop; Record the number of memory blocks applied to the first ordinal apply->blk_num = blk_num_want; Record the number of memory blocks requested for (find = 0; find < apply->blk_num; find++) {Dmems . tb_blk[loop + Find] = dmem_used; } apply->used = dmem_used; The mark request form has been used dmems.apply_num + = 1; Dmems.blk_num + = Blk_num_want; return user; } else {//The free memory block found is not enough, from the next start to find the loop + = find; }}}//Search the entire memory block, no space is found for the size to return NULL;} void Dynmemput (DMEM *user) {uint16_t loop = 0; If the argument is null, return directly if (NULL = = user) {return;} Free memory space for (loop = dmems.tb_apply[user->tb].blk_s; Loop < dmems.tb_apply[user->tb].blk_s + Dmems.tb_apply[us er->tb].blk_num; loop++) {Dmems.tb_blk[loop] = Dmem_free; Dmems.blk_num-= 1; }//Release request Form dmems.tb_apply[user->tb].used = Dmem_free; Dmems.apply_num-= 1;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A simple and powerful microcontroller memory manager-without memory defragmentation