#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "assert.h"
typedef struct STACK_S
{
int StackSize;
char* Addr;
char* StackTop;
char* StackButton;
}STACK_T;
static STACK_T StackBuff;
int Stack_Init_API( int size )
{
if(size < 0)
{
printf("Invalid size\n");
return -1;
}
if((StackBuff.Addr = malloc(size)) == NULL)
{
return -1;
}
memset(StackBuff.Addr, 0, size);
StackBuff.StackSize = size;
StackBuff.StackButton = StackBuff.Addr + size - 1;
StackBuff.StackTop = StackBuff.StackButton;
printf("StackBuff.Addr = %d\nStackBuff.StackButton = %d\nStackBuff.StackTop = %d\n", StackBuff.Addr, StackBuff.StackButton, StackBuff.StackTop);
return 0;
}
int Stack_Write_API(STACK_T * StackBuff, char *pData, int len)
{
if( (StackBuff == NULL) || (len < 0) || (pData == NULL) )
{
printf("Invalid input param\n");
return -1;
}
if((len > StackBuff->StackSize))
{
printf("Write stack data overflow\n");
return -1;
}
if((StackBuff->StackTop - StackBuff->Addr) < len )
{
memmove(StackBuff->StackTop, len + StackBuff->Addr, StackBuff->StackSize - len);
}
memcpy(StackBuff->StackTop - len, pData, len);
StackBuff->StackTop = StackBuff->StackTop - len;
assert( (StackBuff->StackTop >= StackBuff->Addr) );
assert( (StackBuff->StackTop <= StackBuff->StackButton));
printf("StackBuff->StackTop = %d\n StackBuff->StackButton = %d\n", StackBuff->StackTop, StackBuff->StackButton);
return 0;
}
static void Debug_Print_FUN(char *Tmp, int len)
{
int iTmp = 1;
if((Tmp == NULL) || (len < 0))
{
return;
}
for(iTmp; iTmp < len +1; iTmp++)
{
if((iTmp % 0x04) == 0)
{
printf("\t");
}
if((iTmp % 0x10) == 0)
{
printf("\n");
}
printf("%02x", Tmp[iTmp -1]);
}
printf("\n");
}
int Stack_Read_API(STACK_T * StackBuff, char *pData, int len)
{
if( (StackBuff == NULL) || (len < 0) || (pData == NULL) )
{
printf("Invalid input param\n");
return -1;
}
if(StackBuff->StackTop == StackBuff->StackButton)
{
printf("Stack is free\n");
return -1;
}
if((StackBuff->StackTop - StackBuff->StackButton + 1) < len)
{
memcpy(pData, StackBuff->StackTop, StackBuff->StackTop - StackBuff->StackButton + 1);
Debug_Print_FUN(pData, StackBuff->StackTop - StackBuff->StackButton + 1);
return StackBuff->StackTop - StackBuff->StackButton + 1;
}
else
{
memcpy(pData, StackBuff->StackTop, len);
Debug_Print_FUN(pData, len);
return len;
}
}
void Stack_Clear_API(STACK_T * StackBuff)
{
memset(StackBuff->Addr, 0, StackBuff->StackSize);
StackBuff->StackButton = StackBuff->Addr + size - 1;
StackBuff->StackTop = StackBuff->StackButton;
}
void Stack_Free_API( STACK_T * StackBuff )
{
StackBuff->StackButton = NULL;
StackBuff->StackTop = NULL;
free(StackBuff->Addr);
}
int main( void )
{
char *buff, *rbuff;
int i = 0;
Stack_Init_API(120);
buff = malloc(17);
for(;;)
{
memset(buff, i++, 17);
Stack_Write_API(&StackBuff, buff, 17);
rbuff = malloc(StackBuff.StackTop - StackBuff.StackButton + 1);
Stack_Read_API(&StackBuff, rbuff, StackBuff.StackTop - StackBuff.StackButton + 1);
free(rbuff);
}
return 0;
}
#include "string.h"
#include "stdlib.h"
#include "assert.h"
typedef struct STACK_S
{
int StackSize;
char* Addr;
char* StackTop;
char* StackButton;
}STACK_T;
static STACK_T StackBuff;
int Stack_Init_API( int size )
{
if(size < 0)
{
printf("Invalid size\n");
return -1;
}
if((StackBuff.Addr = malloc(size)) == NULL)
{
return -1;
}
memset(StackBuff.Addr, 0, size);
StackBuff.StackSize = size;
StackBuff.StackButton = StackBuff.Addr + size - 1;
StackBuff.StackTop = StackBuff.StackButton;
printf("StackBuff.Addr = %d\nStackBuff.StackButton = %d\nStackBuff.StackTop = %d\n", StackBuff.Addr, StackBuff.StackButton, StackBuff.StackTop);
return 0;
}
int Stack_Write_API(STACK_T * StackBuff, char *pData, int len)
{
if( (StackBuff == NULL) || (len < 0) || (pData == NULL) )
{
printf("Invalid input param\n");
return -1;
}
if((len > StackBuff->StackSize))
{
printf("Write stack data overflow\n");
return -1;
}
if((StackBuff->StackTop - StackBuff->Addr) < len )
{
memmove(StackBuff->StackTop, len + StackBuff->Addr, StackBuff->StackSize - len);
}
memcpy(StackBuff->StackTop - len, pData, len);
StackBuff->StackTop = StackBuff->StackTop - len;
assert( (StackBuff->StackTop >= StackBuff->Addr) );
assert( (StackBuff->StackTop <= StackBuff->StackButton));
printf("StackBuff->StackTop = %d\n StackBuff->StackButton = %d\n", StackBuff->StackTop, StackBuff->StackButton);
return 0;
}
static void Debug_Print_FUN(char *Tmp, int len)
{
int iTmp = 1;
if((Tmp == NULL) || (len < 0))
{
return;
}
for(iTmp; iTmp < len +1; iTmp++)
{
if((iTmp % 0x04) == 0)
{
printf("\t");
}
if((iTmp % 0x10) == 0)
{
printf("\n");
}
printf("%02x", Tmp[iTmp -1]);
}
printf("\n");
}
int Stack_Read_API(STACK_T * StackBuff, char *pData, int len)
{
if( (StackBuff == NULL) || (len < 0) || (pData == NULL) )
{
printf("Invalid input param\n");
return -1;
}
if(StackBuff->StackTop == StackBuff->StackButton)
{
printf("Stack is free\n");
return -1;
}
if((StackBuff->StackTop - StackBuff->StackButton + 1) < len)
{
memcpy(pData, StackBuff->StackTop, StackBuff->StackTop - StackBuff->StackButton + 1);
Debug_Print_FUN(pData, StackBuff->StackTop - StackBuff->StackButton + 1);
return StackBuff->StackTop - StackBuff->StackButton + 1;
}
else
{
memcpy(pData, StackBuff->StackTop, len);
Debug_Print_FUN(pData, len);
return len;
}
}
void Stack_Clear_API(STACK_T * StackBuff)
{
memset(StackBuff->Addr, 0, StackBuff->StackSize);
StackBuff->StackButton = StackBuff->Addr + size - 1;
StackBuff->StackTop = StackBuff->StackButton;
}
void Stack_Free_API( STACK_T * StackBuff )
{
StackBuff->StackButton = NULL;
StackBuff->StackTop = NULL;
free(StackBuff->Addr);
}
int main( void )
{
char *buff, *rbuff;
int i = 0;
Stack_Init_API(120);
buff = malloc(17);
for(;;)
{
memset(buff, i++, 17);
Stack_Write_API(&StackBuff, buff, 17);
rbuff = malloc(StackBuff.StackTop - StackBuff.StackButton + 1);
Stack_Read_API(&StackBuff, rbuff, StackBuff.StackTop - StackBuff.StackButton + 1);
free(rbuff);
}
return 0;
}