📄 sralloc.cpp
字号:
/*****************************************************************************
*
* Simple SRAM Dynamic Memory Allocation
*
*****************************************************************************
* FileName: sralloc.c
* Dependencies:
* Processor: PIC18F with CAN
* Compiler: C18 02.20.00 or higher
* Linker: MPLINK 03.40.00 or higher
* Company: Microchip Technology Incorporated
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the "Company") is intended and supplied to you, the Company's
* customer, for use solely and exclusively with products manufactured
* by the Company.
*
* The software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
*
* This is a simple dynamic memory allocation module. The following are the
* supported services:
*
* unsigned char * NEAR SRAMalloc(NEAR unsigned char nBytes)
* void SRAMfree(unsigned char * NEAR pSRAM)
* void SRAMInitHeap(void)
*
* This version of the dynamic memory allocation limits the segment size
* to 126 bytes. This is specifically designed such to enable better
* performance by limiting pointer manipulation.
*
*
* How it works:
* The model is based on a simple form of a linked list. A block of memory
* refered to as the dynamic heap is split into segments. Each segment
* has a single byte header that references the next segment in the list
* as well as indicating whether the segment is allocated. Consiquently
* the reference implicitly identifies the length of the segment.
*
* This method also enables the possibility of allowing a large number
* of memory allocations. The maximum is limited by the defined heap size.
*
* SRAMalloc() is used to split or merge segments to be allocated.
* SRAMfree() is used to release segments.
*
* Example:
* ----------
* | 0x7F | 0x200 Header Seg1
* | |
* | |
* | |
* | |
* | |
* | |
* | 0x89 | 0x27F Header Seg2 (allocated)
* | |
* | |
* | 0x77 | 0x288 Header Seg3
* | |
* | |
* | |
* | |
* | |
* | |
* | |
* | 0x00 | 0x2FF Tail
* ----------
*
*
* Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
*
* Alloc ------------- reference to next Header --------------
*
*
* Recomendations:
* Although this model will allow dynamic allocation down to a single byte,
* doing so sacrifices performance. With more segments within the heap, more
* time is required to attempt to allocate memory. Plus every segment requires
* a header byte; therefore, smaller segments require more memory. There is
* also the possibility of fragmentation, which could ultimately doom an
* application by reducing the largest allocatable block of memory. Thus the
* recomendation is to allocate at least 8 bytes of memory.
*
*
*
* Author Date Version Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Ross Fosler 05/25/03 v1.03 ... First release
* Nilesh Rajbharti 7/14/04 Modified for Zigbee stack.
* Nilesh Rajbharti 11/1/04 Pre-release version
* DF/KO 04/29/05 Microchip ZigBee Stack v1.0-2.0
* DF/KO 07/18/05 Microchip ZigBee Stack v1.0-3.0
* DF/KO 07/27/05 Microchip ZigBee Stack v1.0-3.1
* DF/KO 08/19/05 Microchip ZigBee Stack v1.0-3.2
* DF/KO 01/09/06 Microchip ZigBee Stack v1.0-3.5
* DF/KO 08/31/06 Microchip ZigBee Stack v1.0-3.6
*****************************************************************************/
#include "Zigbee.def"
#include "Compiler.h"
#include "Generic.h"
/********************************************************************
Compilation options
********************************************************************/
// Enable this switch to output debug messages
//#define ENABLE_DEBUG
// Enabled this switch if 16-bit math is going to be performed during
// an interrupt, and the math temporary variables are not being saved
// on the stack. If in doubt, enable this switch.
#define MAKE_INTERRUPT_SAFE
/********************************************************************
Constants
********************************************************************/
#define NEAR
#define _MAX_SEGMENT_SIZE 127
#define _MAX_HEAP_SIZE MAX_HEAP_SIZE-1
//----------------------------------------self defines by lq---------
extern _LATABITS LATAbits;
extern _INTCONBITS INTCONbits;
extern _RCONBITS RCONbits;
extern _SSPCON1BITS SSPCON1bits;
extern _SSPCON2BITS SSPCON2bits;
extern _PIR1BITS PIR1bits;
extern _LATCBITS LATCbits;
extern _TRISCBITS TRISCbits;
extern _INTCON2BITS INTCON2bits;
extern _SSPSTATBITS SSPSTATbits;
extern _TRISBBITS TRISBbits;
extern _PORTBBITS PORTBbits;
extern _PIE2BITS PIE2bits;
extern _PIR2BITS PIR2bits;
extern _T3CONBITS T3CONbits;
extern _EECON1BITS EECON1bits;
extern int SSPSTAT;
extern int SSPCON1;
extern int ADCON1;
extern int LATA;
extern int TRISA;
extern int LATB;
extern int PORTB;
extern int TMR0L;
extern int T3CON;
extern int TMR3L;
extern int TMR3H;
extern int CCP2CON;
extern int EECON1;
extern int TBLPTR;
extern int EECON2;
extern int TABLAT;
extern void CLRWDT();
extern void NOP();
//----------------------------------------------------------------
/*********************************************************************
* Segment header data type
********************************************************************/
typedef union _SALLOC
{
unsigned char byte;
struct _BITS
{
unsigned count:7;
unsigned alloc:1;
}bits;
}SALLOC;
/*********************************************************************
* Reserve the memory heap
********************************************************************/
#if defined(MCHP_C18)
// NOTE - The starting location here must align with the linker script.
#pragma udata MultiBankHeap=HEAP_LOCATION
unsigned char _uDynamicHeap[MAX_HEAP_SIZE];
#pragma udata
#elif defined(HITECH_C18)
#pragma psect bss=MulitBankHeap
unsigned char _uDynamicHeap[MAX_HEAP_SIZE];
#else
#endif
/*********************************************************************
* Private function declarations
********************************************************************/
BOOL _SRAMmerge(SALLOC * NEAR pSegA);
/*********************************************************************
* Function: unsigned char * SRAMalloc(unsigned char length)
*
* PreCondition: A memory block must be allocated in the linker,
* and the memory headers and tail must already be
* set via the function SRAMInitHeap().
*
* Input: unsigned char nBytes - Number of bytes to allocate.
*
* Output: unsigned char * - A pointer to the requested block
* of memory.
*
* Side Effects:
*
* Overview: This functions allocates a chunk of memory from
* the heap. The maximum segment size for this
* version is 126 bytes. If the heap does not have
* an available segment of sufficient size it will
* attempt to create a segment; otherwise a NULL
* pointer is returned. If allocation is succeessful
* then a pointer to the requested block is returned.
*
* Note: The calling function must maintain the pointer
* to correctly free memory at runtime.
********************************************************************/
#include "console.h"
unsigned char * NEAR SRAMalloc(NEAR unsigned char nBytes)
{
// return alloc(nBytes);
SALLOC * NEAR pHeap;
#ifdef MAKE_INTERRUPT_SAFE
BYTE saveGIEH;
#endif
NEAR SALLOC segHeader;
NEAR unsigned char segLen;
SALLOC * NEAR temp;
#ifdef MAKE_INTERRUPT_SAFE
saveGIEH = GIEH;
GIEH = 0;
#endif
#ifdef ENABLE_DEBUG
ConsolePutROMString( (ROM char * const)"\r\nAlloc " );
PrintChar(nBytes);
#endif
// Do not allow allocation above the max minus one bytes. Also, do
// not allow a zero length packet. Could cause problems.
if ((nBytes > (_MAX_SEGMENT_SIZE - 1)) ||
(nBytes == 0))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -