don't use malloc directry

This commit is contained in:
SANO Masatoshi 2014-08-16 20:54:38 +09:00
parent 7afda003e1
commit c81e18a758
5 changed files with 68 additions and 32 deletions

View file

@ -39,7 +39,7 @@ int cmd_run(int argc,char **argv)
break;
}
}
arg=malloc(sizeof(char*)*(offset+2+argc));
arg=alloc(sizeof(char*)*(offset+2+argc));
arg[0]=bin;
if(core_p) {
offset=2;
@ -50,7 +50,7 @@ int cmd_run(int argc,char **argv)
}else if (strcmp(impl,"native")==0) {
bin= which(version);
if(strcmp(bin,"")!=0) {
arg=malloc(sizeof(char*)*(offset+2+argc));
arg=alloc(sizeof(char*)*(offset+2+argc));
arg[0]=bin;
}else {
printf("can't find %s.\n",version);

View file

@ -9,7 +9,7 @@ struct attr {
};
struct Cons* attralloc(void) {
struct attr* ret=malloc(sizeof(struct attr));
struct attr* ret=alloc(sizeof(struct attr));
ret->name=NULL;
ret->value=NULL;
return toPointer(cons(ret,(LVal)NULL));
@ -29,7 +29,7 @@ void attrsfree(struct Cons* a) {
struct attr* p=(struct attr*)firstp((LVal)a);
s(p->name),s(p->value);
next=a->next;
free(a);
dealloc(a);
a=next;
}
}
@ -121,7 +121,7 @@ struct tag {
};
struct Cons* tagalloc(void) {
struct tag* t=malloc(sizeof(struct tag));
struct tag* t=alloc(sizeof(struct tag));
t->type=0;
t->name=NULL;
t->attr=NULL;
@ -132,8 +132,8 @@ void tagfree(LVal l) {
struct tag* t=firstp(l);
s(t->name);
attrsfree(t->attr);
free(t);
free((void*)l);
dealloc(t);
dealloc((void*)l);
}
void tagsfree(struct Cons* t) {

View file

@ -8,18 +8,18 @@ void free_opts(struct opts* opt)
void* tmp;
while(opt) {
if(opt->value) {
free((void*)opt->value);
dealloc((void*)opt->value);
opt->value=NULL;
}
if(opt->name) {
free((void*)opt->name);
dealloc((void*)opt->name);
opt->name=NULL;
}
tmp=opt->next;
opt->next=NULL;
tmp=opt;
opt=opt->next;
free(tmp);
dealloc(tmp);
}
}
@ -59,7 +59,7 @@ struct opts* load_opts(const char* path) {
while(fgets(buf,1024,fp) !=NULL) {
int i,mode,last;
char* str;
cur->next=(struct opts*)malloc(sizeof(struct opts));
cur->next=(struct opts*)alloc(sizeof(struct opts));
cur=cur->next;
cur->type=OPT_VOID;
cur->value=NULL;
@ -119,7 +119,7 @@ int set_opt(struct opts** opts,const char* name,char* value,int type)
opt=opt->next;
}
if(!found) {
opt=(struct opts*)malloc(sizeof(struct opts));
opt=(struct opts*)alloc(sizeof(struct opts));
opt->next=*opts;
opt->type=type;
opt->name=(const char*)remove_char("\n\t",(char*)name);

View file

@ -15,18 +15,29 @@
#include <stdarg.h>
#include "util.h"
void* alloc(size_t bytes) {
void* p=malloc(bytes);
printf("**%d\n",p);
return p;
}
void dealloc(void* f) {
printf("*:%d\n",f);
free(f);
}
char* q(const char* orig) {
char* ret= (char*)malloc(strlen(orig)+1);
char* ret= (char*)alloc(strlen(orig)+1);
strcpy(ret,orig);
return ret;
}
void s(char* f) {
free(f);
dealloc(f);
}
char* s_cat2(char* a,char* b) {
char* ret= (char*)malloc(strlen(a)+strlen(b)+1);
char* ret= (char*)alloc(strlen(a)+strlen(b)+1);
strcpy(ret,a);
strcat(ret,b);
s(a);
@ -82,7 +93,7 @@ char* subseq(char* base,int beg,int end)
}
if(end<=beg)
return NULL;
ret=malloc(end-beg+1);
ret=alloc(end-beg+1);
for(i=0;i<end-beg;++i) {
ret[i]=base[i+beg];
}
@ -104,7 +115,7 @@ char* remove_char(char* items,char* orig)
}
}
}
ret=malloc(j+1-found);
ret=alloc(j+1-found);
for(j=0,k=0;orig[j]!='\0';++j,++k) {
for(i=0;items[i]!='\0';++i) {
ret[k]=orig[j];
@ -366,7 +377,7 @@ char* s_decode(char* str) {
}
}
if(!write) {
ret=malloc(sizeof(char)*(count+1));
ret=alloc(sizeof(char)*(count+1));
ret[count]='\0';
}
}
@ -414,7 +425,7 @@ char** parse_cmdline(char* cmdline,int *argc)
++count;
}
if(!write)
ret=malloc(sizeof(char**)*(count+1));
ret=alloc(sizeof(char**)*(count+1));
}
ret[count]=NULL;
*argc=count;
@ -425,9 +436,9 @@ int free_cmdline(char** argv)
{
char** p;
for(p=argv;*p!=NULL;++p) {
free(*p);
dealloc(*p);
}
free(argv);
dealloc(argv);
return 1;
}
@ -497,11 +508,32 @@ char* which(char* cmd) {
return p2;
}
LVal directory(char* path)
{
//#ifndef _WIN32
LVal ret=0;
DIR* dir=opendir(path);
struct dirent *dirent;
if(dir==NULL)
return 0;
while(dirent=readdir(dir)) {
char* str=q(dirent->d_name);
if(dirent->d_type&DT_DIR) {
str=s_cat2(str,q("/"));
}
ret=conss(str,ret);
}
closedir(dir);
return ret;
//#endif
}
/*list*/
LVal cons(void* v,LVal l)
{
struct Cons* ret=malloc(sizeof(struct Cons));
struct Cons* ret=alloc(sizeof(struct Cons));
ret->val=(LVal)v;
ret->type=0;
ret->next=l;
@ -520,13 +552,12 @@ LVal conss(char* v,LVal l)
LVal nreverse(LVal v)
{
struct Cons* l=toPointer(v);
struct Cons* next;
struct Cons* before;
for(before=NULL;l;l=next) {
next=Next((LVal)l);
l->next=before;
before=l;
LVal next;
LVal before;
for(before=0;v;v=next) {
next=Next(v);
((struct Cons*)v)->next=before;
before=v;
}
return (LVal)before;
}
@ -638,17 +669,16 @@ void sL(LVal v)
struct Cons* next;
struct Cons* l;
switch(v&3) {
case 0: //pointer
case 1: //number
break;
case 2: //string pointer
s(toString(v));
break;
case 3: //builtin structure
case 0: //builtin structure
for(l=toList(v);l;l=next) {
next=Next((LVal)l);
sL(l->val);
free(l);
dealloc(l);
}
break;
}

View file

@ -1,6 +1,9 @@
#ifndef __UTIL_H__
#define __UTIL_H__
#include <stdint.h>
#ifndef _WIN32
#include <dirent.h>
#endif
extern char** argv_orig;
extern int argc_orig;
@ -39,6 +42,8 @@ void print_list(LVal v);
LVal split_string(char* string,char* by);
void sL(LVal l);
void* alloc(size_t bytes);
void dealloc(void* f);
char* q(const char* orig);
void s(char* f);
char* s_cat2(char* a,char* b);
@ -63,5 +68,6 @@ int system_redirect(const char* cmd,char* filename);
char* uname(void);
char* uname_m(void);
char* which(char* cmd);
LVal directory(char* path);
#endif