Пример #1
0
static int po_dir_list(Popot ppdest, Popot ppat, int get_dirs)
/*****************************************************************************
 * int DirList(char ***list, char *wild, Boolean get dirs);
 ****************************************************************************/
{
Popot *pdest;
char *pat;
Names *wld, *w;
int name_count;
long mem_size;
Popot result;
int slen;
char *spt;
Popot *ppp;

if ((pdest = ppdest.pt) == NULL || (pat = ppat.pt) == NULL)
	return(builtin_err = Err_null_ref);
result.min = result.max = result.pt = NULL;
build_wild_list(&wld,pat,get_dirs);
mem_size = 0;
name_count = 0;
w = wld;
while (w != NULL)
	{
	mem_size += sizeof(Popot)+strlen(w->name)+1;
	name_count += 1;
	w = w->next;
	}
if (name_count == 0)
	goto OUT;
result = po_malloc(mem_size);
if (result.pt == NULL)
	{
	name_count = Err_no_memory;
	goto OUT;
	}
ppp = result.pt;
spt = OPTR(result.pt,name_count*sizeof(Popot));
/* set pointer bounds to just Popot array, not string space */
result.max = OPTR(spt,-1);
w = wld;
while (w != NULL)
	{
	slen = strlen(w->name);
	ppp->min = ppp->max = ppp->pt = spt;
	ppp->max = OPTR(ppp->max,slen);
	ppp += 1;
	slen += 1;
	pj_copy_bytes(w->name, spt, slen);
	spt += slen;
	w = w->next;
	}
OUT:
free_wild_list(&wld);
*pdest = result;
return(name_count);
}
void simple_free_test() {
	/* do some mallocs, free it all, do the mallocs again (sbrk should not move) */
	int i, j, len;
	char * bufs[100];
	void * cur_brk;
	for (i = 0; i < 100; i++) {
		len = 100 + i;
		bufs[i] = po_malloc(len);
		for (j = 0; j < len - 1; j++) {
			bufs[i][j] = '0' + (i % 10);
		}
		bufs[i][len - 1] = '\0';
	}
	for (i = 0; i < 100; i++) {
		printf("%02d [0x%08lX]: %s\n", i, (unsigned long)bufs[i], (char *)bufs[i]);
	}
	cur_brk = sbrk(0);
	for (i = 0; i < 100; i++) {
		po_free(bufs[i]);
	}
	for (i = 0; i < 100; i++) {
		len = 100 + i;
		bufs[i] = po_malloc(len);
		for (j = 0; j < len - 1; j++) {
			bufs[i][j] = '0' + (i % 10);
		}
		bufs[i][len - 1] = '\0';
	}
	printf("== sbrk(0): bfr 0x%08lX, after 0x%08lX ==\n",
			(unsigned long)cur_brk,
			(unsigned long)sbrk(0));
	for (i = 0; i < 100; i++) {
		printf("%02d [0x%08lX]: %s\n", i, (unsigned long)bufs[i], (char *)bufs[i]);
	}

}
Пример #3
0
static Popot po_strdup(Popot s)
/*****************************************************************************
 * char *strdup(char *source)
 ****************************************************************************/
{
	int len;
	Popot d = {NULL,NULL,NULL};

	if (s.pt == NULL)
		{
		builtin_err = Err_null_ref;
		}
	else
		{
		len = strlen(s.pt)+1;
		d = po_malloc(len);
		strcpy(d.pt,s.pt);
		}
	return(d);
}
void malloc_only_test() {
	int i, j;
	char * buf;
	char * oldbuf = NULL;
	char * bufs[100 - 2];
	for (i = 2; i < 100; i++) {
		buf = (char *)po_malloc(i);
		if (oldbuf) {
			printf("  diff: %lu\n", (unsigned long)buf - (unsigned long)oldbuf);
		}
		oldbuf = buf;
		bufs[i - 2] = buf;
		for (j = 0; j < i - 1; j++) {
			buf[j] = '0' + (i % 10); // put digit into string
		}
		buf[i - 1] = '\0';  // nul terminate
	}

	for (i = 0; i < 100 - 2; i++) {
		printf("%d -> %s\n", i, bufs[i]);
	}
}