summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lmem.h
blob: 97a888c7f89671ccd04d57777f1b729f78f4d431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
** $Id$
** Interface to Memory Manager
** See Copyright Notice in lua.h
*/

#ifndef lmem_h
#define lmem_h


#include <stddef.h>

#include "llimits.h"
#include "lua.h"

#define MEMERRMSG	"not enough memory"


#define luaM_reallocv(L,b,on,n,e) \
	((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ?  /* +1 to avoid warnings */ \
		luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \
		luaM_toobig(L))

#define luaM_freemem(L, b, s)	luaM_realloc_(L, (b), (s), 0)
#define luaM_free(L, b)		luaM_realloc_(L, (b), sizeof(*(b)), 0)
#define luaM_freearray(L, b, n, t)   luaM_reallocv(L, (b), n, 0, sizeof(t))

#define luaM_malloc(L,t)	luaM_realloc_(L, NULL, 0, (t))
#define luaM_new(L,t)		cast(t *, luaM_malloc(L, sizeof(t)))
#define luaM_newvector(L,n,t) \
		cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))

#define luaM_growvector(L,v,nelems,size,t,limit,e) \
          if ((nelems)+1 > (size)) \
            ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))

#define luaM_reallocvector(L, v,oldn,n,t) \
   ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))


LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
                                                          size_t size);
LUAI_FUNC void *luaM_toobig (lua_State *L);
LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
                               size_t size_elem, int limit,
                               const char *errormsg);

#endif

opt">}; static const char weekdays [7] [10] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; static const char smonths [12] [4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; static const char* months [12] = { "January", "February", "March", "April", smonths[5-1], "June", "July", "August", "September", "October", "November", "December" }; static const char ampm [4] [3] = { "am", "pm", "AM", "PM" }; static void i2a ( char* dest,unsigned long x ) { int div = 10; *dest++ = x/div + '0'; *dest++ = x%div + '0'; *dest++ = '\0'; } size_t strftime ( char* dst, size_t max, const char* format, const struct tm* tm ) { char* p = dst; const char* src; unsigned long no; char buf [5]; if (!max) return 0; for ( ; *format != '\0'; format++ ) { if (*format == '%') { if (*++format == '%') { *p++ = '%'; } else again: switch (*format) { // case '%': *p++ = '%'; break; // reduce size of jump table case 'n': *p++ = '\n'; break; case 't': *p++ = '\t'; break; case 'O': case 'E': ++format; goto again; case 'c': src = "%b %a %d %k:%M:%S %Z %Y"; goto _strf; case 'r': src = "%I:%M:%S %p"; goto _strf; case 'R': src = "%H:%M"; goto _strf; case 'x': src = "%b %a %d"; goto _strf; case 'X': src = "%k:%M:%S"; goto _strf; case 'D': src = "%m/%d/%y"; goto _strf; case 'T': src = "%H:%M:%S"; _strf: p += strftime (p, (size_t)(dst+max-p), src, tm); break; case 'a': src = sweekdays [tm->tm_wday]; goto _str; case 'A': src = weekdays [tm->tm_wday]; goto _str; case 'h': case 'b': src = smonths [tm->tm_mon]; goto _str; case 'B': src = months [tm->tm_mon]; goto _str; case 'p': src = ampm [tm->tm_hour > 12 ? 3 : 2]; goto _str; case 'P': src = ampm [tm->tm_hour > 12 ? 1 : 0]; goto _str; case 'C': no = tm->tm_year/100 + 19; goto _no; case 'd': no = tm->tm_mday; goto _no; case 'e': no = tm->tm_mday; goto _nos; case 'H': no = tm->tm_hour; goto _no; case 'I': no = tm->tm_hour % 12; goto _no; case 'j': no = tm->tm_yday; goto _no; case 'k': no = tm->tm_hour; goto _nos; case 'l': no = tm->tm_hour % 12; goto _nos; case 'm': no = tm->tm_mon + 1; goto _no; case 'M': no = tm->tm_min; goto _no; case 'S': no = tm->tm_sec; goto _no; case 'u': no = tm->tm_wday ? tm->tm_wday : 7; goto _no; case 'w': no = tm->tm_wday; goto _no; case 'U': no = (tm->tm_yday - tm->tm_wday + 7) / 7; goto _no; case 'W': no = (tm->tm_yday - (tm->tm_wday - 1 + 7) % 7 + 7) / 7; goto _no; case 's': { time_t t = #if CONFIG_RTC rb->mktime((struct tm*)tm) #else 0 #endif ; char buf[101]; char* c; buf[100]=0; for (c=buf+99; c>buf; --c) { *c=(t%10)+'0'; t/=10; if (!t) break; } src=c; goto _str; } case 'Z': #ifdef WANT_TZFILE_PARSER tzset(); src = tzname[0]; #else src = "[unknown timezone]"; #endif goto _str; case 'Y': i2a ( buf+0, (unsigned int)(tm->tm_year / 100 + 19) ); i2a ( buf+2, (unsigned int)(tm->tm_year % 100) ); src = buf; goto _str; case 'y': no = tm->tm_year % 100; goto _no; _no: i2a ( buf, no ); /* append number 'no' */ src = buf; goto _str; _nos: i2a ( buf, no ); /* the same, but '0'->' ' */ if (buf[0] == '0') buf[0] = ' '; src = buf; _str: while (*src && p < dst+max) /* append string */ *p++ = *src++; break; }; } else { *p++ = *format; } if (p >= dst+max) break; } *p = '\0'; return p - dst; }