Cortex_m3_stm32 Embedded Learning Note (24): Memory management Experiment (dynamic memory)

Source: Internet
Author: User
Tags define null

Children's shoes, which have been programmed with C language, must know something about dynamic management memory. That's not much of a benefit. Dynamic memory management for STM32 today

Memory management refers to the technology of allocating and using computer memory resources when the software is running. Its primary purpose is to efficiently and quickly allocate and to release and reclaim memory resources when appropriate. There are many ways to implement memory management, and they end up with two functions:malloc and free (well-known); malloc functions for memory requests, and for memory deallocation.

Implementation method: Block-type memory management

As you can see, chunked memory management consists of two parts: the memory pool and the memory management table. The memory pool is divided into n blocks, corresponding memory management tables, and the size is N, and each item in the Memory management table corresponds to a piece of memory in the memory pool. The key value of the memory management table represents the meaning that when the item value is 0, the corresponding memory block is not occupied, and when the key value is nonzero, the memory block that corresponds to the item is already occupied, and its value represents the number of memory blocks that are continuously occupied. For example, if a value is 10, a total of 10 memory blocks are allocated to an external pointer, including the corresponding memory block.
The inner-inch distribution direction is the distribution direction from top to bottom. That is, first find empty memory from the end. When memory management has just been initialized, the memory table is all zeroed, indicating that no memory blocks are occupied.

Allocation principle
When the pointer p calls malloc to request memory, first determine the number of memory blocks p to allocate (m), and then start from the nth item, look down until the M block of contiguous empty memory block (that is, the corresponding memory management table entry is 0), and then the M Memory management table key values are set to M (tag is occupied), and finally, Return the address of the last empty memory block to the pointer p to complete the assignment. Note that if there is not enough memory (and no contiguous m block of free memory is found), NULL is returned to P, indicating that the allocation failed.
Release principle
The free function is called when P is requested to run out of memory and needs to be released. The free function first determines the memory block of the memory address that P points to, then finds the corresponding memory management table item, gets the number of memory blocks occupied by P (the value of the memory management table item is the number of allocated memory blocks), the value of the M Memory Management table entry is zeroed, the token is freed, and a memory release is completed.
code: External two function mymalloc (int size) parameter is the size of the requested memory, returns the first address of the requested memory, and returns NULL if the request fails;myfree (void *p) releases P-headed pointer;
#ifndef __malloc_h#define __malloc_h#include "stm32f10x.h" #ifndef null#define NULL 0#endif//memory parameter settings. #define Mem_block_ SIZE32    //memory block size is 32 bytes # define MEM_MAX_SIZE42*1024  //MAX managed memory 42k#define Mem_alloc_table_sizemem_max_size/mem_ Block_size//Memory table size  //memory Management controller struct _m_mallco_dev{void (*init) (void);//Initialize U8 (*perused) (void);  Memory utilization U8 *membase;//memory pool U16 *memmap; Memory Management State Table U8  Memrdy;//memory management is ready};extern struct _m_mallco_dev mallco_dev;//defines void mallco.c in mymemset (void *s,u8 C, U32 count);//Set memory void mymemcpy (void *des,void *src,u32 N);//Copy Memory     void mem_init (void);//Memory Management initialization function (outer/internal call) U32 Mem_ malloc (u32 size); memory allocation (internal call) U8 Mem_free (u32 offset); Memory free (internal call) U8 mem_perused (void);//Memory usage (outside/internal calls)//user call function void Myfree (void *ptr);  Memory free (external call) void *mymalloc (u32 size);//memory allocation (external call) void *myrealloc (void *ptr,u32 size);//Reallocate memory (external call) #endif

#include "malloc.h"//Memory Pool (4-byte alignment) __align (4) U8 Membase[mem_max_size];//sram memory pool//memory management table U16 Memmapbase[mem_alloc_table_ Size];//sram Memory pool map//Memory Management parameter const U32 memtblsize=mem_alloc_table_size;//memory Table size const U32 memblksize=mem_block_size;// Memory chunk Size const U32 memsize=mem_max_size;//Memory Total size//memory management controller struct _M_MALLCO_DEV mallco_dev={mem_init,//memory initialization mem_perused, Memory usage membase,//memory pool memmapbase,//memory management Status table 0,//memory management not ready};//replication memory//*des: Destination address//*src: Source address//n: Memory length to be copied (bytes) void     mymemcpy (void *des,void *src,u32 N) {U8 *xdes=des;u8 *xsrc=src;  while (n--) *xdes++=*xsrc++;      }//Set memory//*s: Memory first address//c: The value to set//count: The amount of memory that needs to be set (in bytes) void Mymemset (void *s,u8 c,u32 count) {U8 *xs = s;  while (count--) *xs++=c; }//Memory management initialize void Mem_init (void) {Mymemset (Mallco_dev.memmap, 0,memtblsize*2);//Memory state table data Clear 0 mymemset ( Mbase, 0,memsize);//memory pool All data clear 0 mallco_dev.memrdy=1;//memory management initialization OK}//Get memory usage//return value: Usage (0~100) U8 mem_perused (void) {u      Used=0;      U32 i;  for (i=0;i<memtblsize;i++)    {if (mallco_dev.memmap[i]) used++;  } return (USED*100)/(memtblsize);  }//memory allocation (internal call)//memx: Memory block//size: Memory size to allocate (bytes)//return value: 0XFFFFFFFF, representing error, other, memory offset address u32 mem_malloc (u32 size) {Signed long      offset=0;      U16 nmemb;//required number of memory blocks U16 cmemb=0;//contiguous empty memory block number u32 I;  if (!mallco_dev.memrdy) mallco_dev.init ();//uninitialized, first perform initialization if (size==0) return 0xffffffff;//do not need to allocate nmemb=size/memblksize;      Gets the number of contiguous blocks of memory that need to be allocated if (size%memblksize) nmemb++; for (offset=memtblsize-1;offset>=0;offset--)//Search entire memory control area {if (!mallco_dev.memmap[offset]) cmemb++;//number of contiguous empty memory blocks increased ELS                  e cmemb=0;//continuous memory block clear 0 if (CMEMB==NMEMB)//found a continuous nmemb empty memory block {for (i=0;i<nmemb;i++)//callout memory block non-empty {              MALLCO_DEV.MEMMAP[OFFSET+I]=NMEMB; } return (offset*memblksize);//Return offset address}} return 0xffffffff;//not found memory block matching allocation condition}//Free memory (internal call)//OFFSE  T: Memory address offset//return value: 0, Release succeeded; 1, release failed;      U8 Mem_free (u32 offset) {int i; if (!mallco_dev.memrdy)//uninitialized, first perform initialization of {Mallco_dEv.init ();     Return 1;//Uninitialized} if (offset<memsize)//offset within the memory pool. {int index=offset/memblksize;//offset memory block number int nmemb=mallco_dev.memmap[index];//memory block number for (i=0;i&lt          ; nmemb;i++)//memory block clear 0 {mallco_dev.memmap[index+i]=0;      } return 0;  }else return 2;//offsets the hyper-zone.      }//Free memory (external call)//ptr: Memory header address void myfree (void *ptr) {u32 offset;   The IF (ptr==null) return;//address is 0.      offset= (u32) ptr-(u32) mallco_dev.membase; Mem_free (offset);//Free memory}//Allocate memory (external call)//size: Memory size (bytes)//return value: The first address assigned to the memory. void *mymalloc (u32 size)//return any type of pointer (or address?).        ) {u32 offset;            Offset=mem_malloc (size);      if (OFFSET==0XFFFFFFFF) return NULL;  else return (void*) ((u32) mallco_dev.membase+offset);      }//Reallocate memory (external call)//*ptr: Old memory header address//size: Memory size to allocate (bytes)//return value: The newly assigned memory header address. Void *myrealloc (void *ptr,u32 size) {u32 offset;      Offset=mem_malloc (size);         if (OFFSET==0XFFFFFFFF) return NULL; else {mymemcpy (void*) ((UMallco_dev.membase+offset), ptr,size);//copy old memory contents to new memory Myfree (PTR);  Release old memory Return (void*) ((u32) mallco_dev.membase+offset); return new memory First address}}

Main function test:
#include "led.h" #include "delay.h" #include "sys.h" #include "usart.h" #include "lcd.h" #include "key.h" #include "  Malloc.h "#include" usmart.h "int main (void) {U8 key;    U8 i=0; U8 *p=0;u8 *tp=0;u8 paddr[18];//stores the ASCII value of the P-addr:+p address nvic_configuration ();     Delay_init (); The delay function initializes the uart_init (9600);  The serial port is initialized to 9600led_init ();   Initialize the hardware interface with the LED connection lcd_init (); Initialize Lcdusmart_dev.init (72);//Initialize Usmart key_init ();//Key to initialize Mem_init ();//Initialize memory pool point_color=red;//set font to red Lcd_ Showstring (60,50,200,16,16, "Mini STM32"); Lcd_showstring (60,70,200,16,16, "MALLOC TEST"); Lcd_showstring (60,90,200,16,16, "[email protected]");  Lcd_showstring (60,110,200,16,16, "2014/3/12"); Lcd_showstring (60,130,200,16,16, "Key0:malloc"); Lcd_showstring (60,150,200,16,16, "Key1:write Data"); Lcd_showstring (60,170,200,16,16, "Wk_up:free");    point_color=blue;//set the font to Blue lcd_showstring (60,190,200,16,16, "SRAM used:%"); while (1) {key=key_scan (0);//Do not support double press switch (key) {case 0://no key press break;case 1://key0 Press P=mymalloc (2048);//Request 2K byte if (p!= NULL) sprintf ((char*) p, "Memory Malloc test%03d", i);//write some content to P Break;case 2://key1 Press if (p!=null) {sprintf ((char*) p, " Memory Malloc test%03d ", i);//Update display content lcd_showstring (60,250,200,16,16,P); Display the contents of P}break;case 3://wk_up Press Myfree (p);//Release memory p=0;//point to empty address break; }if (tp!=p) {tp=p;sprintf ((char*) paddr, "P-addr:0x%08x", (U32) TP); Lcd_showstring (60,230,200,16,16,PADDR);//show P's address if (p) lcd_showstring (60,250,200,16,16,p);//display P's content else Lcd_fill (   60,250,239,266,white);//p=0, clear display}delay_ms (10); I++;if ((i%20) ==0)//ds0 blinking. {Lcd_shownum (60+80,190,mem_perused (), 3,16);//Display Memory Utilization led0=! LED0; }}    }

Cortex_m3_stm32 Embedded Learning Note (24): Memory management Experiment (dynamic memory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.