Keil c51 Internal RAM (idata) dynamic memory management Program

Source: Internet
Author: User

Source: Keil c51 internal RAM (idata) dynamic memory management Program

The procedure is relatively simple, but the feeling is more interesting, the individual thinks has the certain application value, hoped everybody has the better thought and the method, promotes mutually.
The basic idea of the program is that in the Ram area above the CPU stack pointer sp, by moving the stack pointer sp up to a few bytes, the vacated Ram area is available to the user, and when the user is finished, the Ram area can be released.
Header file Dmalloc51.h

/***********************************************************************************************************                              C51 internal RAM dynamic memory request function, dynamic memory release function * (c) Copyright 2004.6, lm7556,china* All rights reserved*** file: dmalloc51.h**************************************************************************** ******************************///The dynamic memory request function dmem8u *dmalloc (dmem8u dmsize,stksize) opens a space in the RAM stack to do the variable area.//The dynamic memory deallocation function dmem8u freedmalloc (dmem8u *dmp,dmem8u dmsize) frees the dynamic memory allocated space from the stack. /******************************** Define Messages *****************************************/#defineCpu_pclen 2//The length of the CPU program pointer (in bytes).#defineRam_size 0x100//The number of RAM bytes inside the CPU. #defineMem_over 0xFF//CPU Internal RAM memory is not enough. #defineNo_mem_del 0xFE//an attempt was made to free memory space that does not exist. #definemem_deleted 0XFD//The memory space was released successfully. typedef unsignedCharidata dmem8u;/************* Dynamic Memory request function dmem8u *dmalloc (dmem8u dmsize,stksize) *********************///This function moves the stack sp up dmsize byte space, and then returns a pointer to the start address of the space .//The entry parameter; Dmsize is the amount of space that needs to be allocated, in dmem8u units. //stksize The stack space that needs to be reserved for the CPU.//exit Parameters: Return stack_over---CPU internal RAM stack overflow, allocated space does not exist.//If successful, returns a pointer to the starting address of the allocated space (size dmsize). DMEM8U *Dmalloc (dmem8u dmsize,stksize);/************ Dynamic Memory release function Freedmalloc (dmem8u *dmp,dmem8u dmsize) *********************///The function frees the space (size dmsize) of the starting address as DMP from the stack. //The entry parameter; Dmsize is the amount of space that needs to be freed, in dmem8u units. //the DMP pointer points to the starting address where the space needs to be freed.//exit Parameters: Return mem_delete---Memory space is released successfully.//returns No_mem_del---attempt to free memory space that does not exist.dmem8u Freedmalloc (dmem8u *dmp,dmem8u dmsize);

Main file: dmalloc51.c

/*********************************************************************************************************** C51 Internal RAM Dynamic request function, dynamic release function * (c) Copyright 2004.6, lm7556,china* all R ights reserved*** File: dmalloc51.c******************************************************************************** **************************/#include"Dmalloc51.h"SFR SP=0x81;/************* Dynamic Memory request function dmem8u *dmalloc (dmem8u dmsize,stksize) *********************///The dynamic memory request function dmem8u *dmalloc (dmem8u dmsize,stksize) opens a space in the RAM stack to do the variable area.//This function moves the stack sp up dmsize byte space, and then returns a pointer to the start address of the space .//The entry parameter; Dmsize is the amount of space that needs to be allocated, in dmem8u units. //stksize The stack space that needs to be reserved for the CPU.//exit Parameters: Return stack_over---CPU internal RAM stack overflow, allocated space does not exist.//If successful, returns a pointer to the starting address of the allocated space (size dmsize). DMEM8U *Dmalloc (dmem8u dmsize,stksize) {dmem8u*p1,*p2,spbuf; UnsignedintStkchk; Stkchk=sp+dmsize+stksize; if(Stkchk> (ram_size-1)) {returnMem_over;}//Check to see if the memory you want to apply exists.spbuf=SP; Spbuf-=Cpu_pclen; P1= (dmem8u*) Spbuf; P2=p1+dmsize; Spbuf= (dmem8u) p2+Cpu_pclen; SP=Spbuf;//The return address of the upper function is moved to the top of the space, so that the function can return to the upper function correctly after execution.p1++;p 2++; *p2++=*p1++; *p2=*P1; P1--; returnp1;}/************ Dynamic Memory release function Freedmalloc (dmem8u *dmp,dmem8u dmsize) *********************///The dynamic memory deallocation function dmem8u freedmalloc (dmem8u *dmp,dmem8u dmsize) frees the dynamic memory allocated space from the stack. //The function frees the space (size dmsize) of the starting address as DMP from the stack. //The entry parameter; Dmsize is the amount of space that needs to be freed, in dmem8u units. //the DMP pointer points to the starting address where the space needs to be freed.//exit Parameters: Return mem_delete---Memory space is released successfully.//returns No_mem_del---attempt to free memory space that does not exist.dmem8u Freedmalloc (dmem8u *dmp,dmem8u dmsize) {dmem8u*p1,*P2,spbuf,i; Spbuf= (dmem8u) dmp+dmsize; if(spbuf<= (dmem8u) DMP) {returnNo_mem_del;} if(Spbuf>sp-cpu_pclen) {spbuf=sp-Cpu_pclen;} I=sp-Spbuf; P1= (dmem8u*) Spbuf; P1++; P2=DMP; Spbuf= (SP-SPBUF) +P2; Spbuf--;  while(i) {*p2++=*p1++;//move the return address of the upper-level function to the bottom of the space, allowing the program to return to the upper-level function.i--; } SP=Spbuf; returnmem_deleted;}

Application Examples:

Files: main.c

#include <reg52.h>#include<stdio.h>#include<intrins.h>#defineStack_size 0x30//the minimum stack reserved for the program.#include"Dmalloc51.h"voidInitsio (void);voidTdelay (unsignedintt);voidFn_drama (void);voidFn_dramb (void);voidFN_DRAMC (void);voidMainvoid) {Initsio ();  while(1) {Tdelay ( -);        Fn_drama (); printf ("\ n"); Tdelay ( -);        Fn_dramb (); printf ("\ n"); Tdelay ( -);        FN_DRAMC (); printf ("\ n"); }}#defineDmsize_a 0x20voidFn_drama (void) {unsignedChari,*mp,*MPP; if((Mpp=dmalloc (dmsize_a,stack_size)) ==mem_over) {return;}; MP=MPP;  for(i=0; i<dmsize_a-1; i++)    {        *mp++=i+0x20; }    *mp=0;    printf (MPP); printf ("\ n");    Fn_dramb ();    Freedmalloc (mpp,dmsize_a); //back plus NOP () to prevent C51 from making Freedmalloc (MPP) LJMP calls. _nop_ ();}#defineDmsize_b 0x20voidFn_dramb (void) {unsignedChari,*mp,*MPP; if((Mpp=dmalloc (dmsize_b,stack_size)) ==mem_over) {return;}; MP=MPP;  for(i=0; i<dmsize_b-1; i++)    {        *mp++=i+0x40; }    *mp=0;    printf (MPP); printf ("\ n");    FN_DRAMC ();    Freedmalloc (Mpp,dmsize_b); //back plus NOP () to prevent C51 from making Freedmalloc (MPP) LJMP calls. _nop_ ();}#defineDmsize_c 0x20voidFN_DRAMC (void) {unsignedChari,*mp,*MPP; if((Mpp=dmalloc (dmsize_c,stack_size)) ==mem_over) {return;}; MP=MPP;  for(i=0; i<dmsize_c-1; i++)    {        *mp++=i+0x60; }    *mp=0;    printf (MPP); printf ("\ n");    Freedmalloc (Mpp,dmsize_c); //back plus NOP () to prevent C51 from making Freedmalloc (MPP) LJMP calls. _nop_ ();}voidTdelay (unsignedintt) {unsignedinti,j;  for(i=0; i<t;i++)    {         for(j=0; j<t;j++) {}    }}voidInitsio (void) {Tmod=tmod&0x0F; Tmod=tmod|0x20; TL1=0xFD, th1=0xFD;//19200, 22.1184MHzscon=0x50; pcon=0x00; TR1=1; TI=1;/*Ti:set TI to send first char of UART*/}

Keil c51 Internal RAM (idata) dynamic memory Management Program (RPM)

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: info-contact@alibabacloud.com 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.