LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2004, 08:51 PM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
how does linux handle memory allocation?


im programming a uri parser which has a lot of pointers and double pointers involved (arrgh)
and I am checking for memory leaks by running the function that does all the allocating then running the function that does all the freeing in an infinite loop and observing the memory usage in the Info Center.


When I run the program the memory stays the same for about 10 secs then starts going down rapidly. First of all this seems odd. Why doesnt the memory start leaking immediately. I also can't *find* any memory leeks, although its nearly 3am too ;0

anyone who knows a little about linux process memory organisation?
 
Old 04-17-2004, 09:01 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
There's a tool called dmalloc that might help. It hijacks malloc(), free(), and friends and can keep track of which lines of your code have allocated memory which was never freed.
 
Old 04-17-2004, 09:02 PM   #3
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
im using electricfence to check for violations. Ill check that dmalloc, thanks
 
Old 04-17-2004, 09:25 PM   #4
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
well for anyone felling up to some debugging, or if you just want to read my code, here it is
Code:
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


typedef struct _authority
{
}
authority;

typedef struct _net_path
{
	authority * auth;
	char ** abspathsegs;
}
net_path;

typedef struct _hier_part
{
	char ** abspathsegs;
	char * query;
}
hier_part;

typedef struct _parseduri
{
	char * scheme;
	char * opaque_part;
	hier_part * hierpart;
}
parseduri;

bool is_reserved(char c)
{
	if (c==';' || c== '/' || c=='?' || c==':' || c=='@' ||
	 c=='&' || c=='=' || c== '+' || c=='$' || c==',') return true; else return false;
}

bool is_mark(char c)
{
	if (c=='-' || c== '_' || c=='.' || c=='!' || c=='~' || c=='*' || c=='\'' ||
	 c== '(' || c==')' ) return true; else return false;
}

bool is_upalpha(char c)
{
	if (c>=65 && c<=90) return true; else return false;
}

bool is_lowalpha(char c)
{
	if (c>=97 && c<=122) return true; else return false;
}

bool is_digit(char c)
{
	if (c>=48 && c<=57) return true; else return false;
}

bool is_alpha(char c)
{
	if (is_upalpha(c) || is_lowalpha(c)) return true; else return false;
}

bool is_alphanum(char c)
{
	if (is_alpha(c) || is_digit(c)) return true; else return false;
}

bool is_unreserved(char c)
{
	if (is_alphanum(c) || is_mark(c)) return true; else return false;
}

bool is_hex(char c)
{
	if (is_digit(c) 
	||c=='A'||c=='B'||c=='C'||c=='D'||c=='E'||c=='F'
	||c=='a'||c=='b'||c=='c'||c=='d'||c=='e'||c=='f'
	) return true; else return false;
}

bool is_escaped(char * c)
{
	if (*c=='%') if (is_hex(*(c+1))) if (is_hex(*(c+2))) return true;
	return false;
}































void freeptrs(void ** ptrs)
{
	while (*ptrs)
	{
		free(*ptrs);
		*ptrs=NULL;
		ptrs++;
	}
}
void freehier_part(hier_part * hier_part)
{
	if (hier_part->abspathsegs)
	{
		freeptrs((void**)hier_part->abspathsegs);
		printf(" freed *abspathsegs\n");
		free(hier_part->abspathsegs); 
		printf(" freed abspathsegs\n");
		hier_part->abspathsegs=NULL; 
	}
	if (hier_part->query)
	{
		free(hier_part->query);
		printf(" freed query\n");
		hier_part->query=NULL;
	}
}
void freeparseduri(parseduri * parseduri)
{
	if (parseduri->scheme)
	{
		free(parseduri->scheme);
		printf("freed scheme\n");
		parseduri->scheme=NULL;
	}
	if (parseduri->opaque_part)
	{
		free(parseduri->opaque_part);
		printf("freed opaque_part\n");
		parseduri->opaque_part=NULL;
	}
	if (parseduri->hierpart)
	{
		freehier_part(parseduri->hierpart); 
		printf("freed hier_part\n");
		parseduri->hierpart=NULL;
	}
}


















void nullparseduri(parseduri * parseduri)
{
	parseduri->scheme=NULL;
	parseduri->opaque_part=NULL;
	parseduri->hierpart=NULL;
}

void nullhier_part(hier_part * hier_part)
{
	hier_part->abspathsegs=NULL;
	hier_part->query=NULL;
}












void setscheme(char * firstchar,char * lastchar,char * &destloc)
{
	/* if this function is successful the scheme name will be stored at destloc (NULL-terminated),otherwise
	destloc will me made to NULL point. */
	char * mrkr1=firstchar;
	bool ok;
	
	/* its not ok if the string is of length 0 */
	if (firstchar>lastchar) ok=false; else ok=true;
		
	while (mrkr1<=lastchar && ok)
	{
		if (mrkr1==firstchar) { if (!is_alpha(*mrkr1)) ok=false; }
		else { if (!(is_alphanum(*mrkr1) || *mrkr1=='+' || *mrkr1=='-' || *mrkr1=='.')) ok=false; }	
		mrkr1++;
	}
	
	
	
	if (ok)
	{
		destloc=(char*)malloc((lastchar-firstchar)+2);
		strncpy(destloc,firstchar,(lastchar-firstchar)+1);
		*(destloc+((lastchar-firstchar)+1))=NULL;
		
		/* convert any uppercase chars to lowercase */
		mrkr1=destloc;
		while (*mrkr1!=NULL)
		{
			if (*mrkr1>=65 && *mrkr1<=90) *mrkr1+=32;
			mrkr1++;
		}
	}
	else
	{
		destloc=NULL;
	}
}

void setabspath(char * firstchar,char * lastchar,char ** &destlocs)
{
	unsigned long numsegs=1;
	unsigned long x;
	char * mrkr1=firstchar;
	char * mrkr2=mrkr1;
	while (*mrkr2!=NULL && *mrkr2!='?') 
	{
		if (*mrkr2=='/' && *(mrkr2+1)!=NULL && *(mrkr2+1)!='?') numsegs++;
		mrkr2++;
	}
		
	/* allocate space for the pointers, including 1 ((NULL-pointing) terminating pointer) */
	destlocs=(char**)malloc((sizeof(char*)*(numsegs+1)));
	
	/* since we know the # of pointers in advance, we will set the NULL terminator right now */
	*(destlocs+numsegs)=NULL;
	
	/* allocate the actual segments */
	mrkr2=mrkr1;
	x=0;
	while (*mrkr1!=NULL && *mrkr1!='?' && (x<numsegs))
	{
		if (*mrkr2=='/' || *mrkr2==NULL || *mrkr2=='?')
		{
			if (x>0) mrkr1++; 
			*(destlocs+x)=(char*)malloc((mrkr2-mrkr1)+1);
			strncpy(*(destlocs+x),mrkr1,(mrkr2-mrkr1));
			*((*(destlocs+x))+(mrkr2-mrkr1))=NULL;
			x++;
			mrkr1=mrkr2;
		}
		mrkr2++;
	}
}

bool parseuri(char * uri,parseduri * parseduri)
{
	char * mrkr1,* mrkr2,* mrkr3;
	
	/* set all pointer fields in the parseduri struct to NULL. This is to facilitate
	the freeparseduri() function so it won't attempt to free un malloc()`d memory */
	nullparseduri(parseduri);
	
	mrkr1=uri;
	mrkr2=uri;
	while (*mrkr2!=':' && *mrkr2!=NULL) mrkr2++;
	if (*mrkr2==NULL) return false;
	mrkr3=mrkr2+1;
	if (*mrkr3==NULL) return false;
	setscheme(mrkr1,mrkr2-1,parseduri->scheme);
	if (!parseduri->scheme) return false;
	
	/* scheme has been set; mrkr3 points to first char after ':', already established not to be NULL */
	if (*mrkr3!='/')
	{
		/* rest of uri is opaque. check that its valid */
		while (*mrkr3!=NULL && (is_reserved(*mrkr3) || is_unreserved(*mrkr3) || is_escaped(mrkr3))) mrkr3++;
		if (*mrkr3!=NULL)
		{
			/* opaque part contained illegal characters */
			freeparseduri(parseduri);
			return false;
		}
		else
		{
			/* opaque part is fine; allocate and copy */
			mrkr2++;
			parseduri->opaque_part=(char*)malloc((mrkr3-mrkr2)+1);
			strcpy(parseduri->opaque_part,mrkr2);
			return true;
		}
	}
	else
	{
		/* rest of uri is hierarchial */

		mrkr3++;
		if (*mrkr3==NULL || *mrkr3=='?')
		{
			/* theres nothing left in the path, it is therefore invalid */
			freeparseduri(parseduri);
			return false;
		}
		
		parseduri->hierpart=(hier_part*)malloc(sizeof(hier_part));
		nullhier_part(parseduri->hierpart);
		
		if (*mrkr3=='/')
		{
			/* net path */
			mrkr3++;
			if (*mrkr3==NULL || *mrkr3=='?')
			{
				/* theres nothing left in the path, it is therefore invalid */
				freeparseduri(parseduri);
				return false;
			}
			/* TODO */
		}
		else
		{
			/* abs path */
			
			/* first make sure the abs_path is valid. (double-slashes are not welcomed) */
			mrkr1=mrkr3;
			while (*mrkr3!=NULL && *mrkr3!='?' && (is_unreserved(*mrkr3) || is_escaped(mrkr3) || 
			*mrkr3==':' || *mrkr3=='@' || *mrkr3=='&' || *mrkr3=='=' || *mrkr3=='+' || *mrkr3=='$' 
			|| *mrkr3==',' || (*mrkr3=='/' && *(mrkr3-1)!='/'))) mrkr3++;
			
			if (*mrkr3==NULL||*mrkr3=='?')
			{
				/* it is valid */
				setabspath(mrkr1,mrkr3-1,parseduri->hierpart->abspathsegs);
			}
			else
			{
				/* its invalid */
				freeparseduri(parseduri);
				return false;
			}
		}
	}
	return true; /* TEMP */
}


int main()
{
	parseduri parseduri;
	
	char uri[1000];
	
	printf("Enter a URI->");
	scanf("%s",uri);
	
	if (parseuri(uri,&parseduri))
	{
		printf("URI was parsed.\n");
		if (parseduri.scheme){ printf("scheme:"); puts(parseduri.scheme); }
		if (parseduri.opaque_part){ printf("opaque_part:"); puts(parseduri.opaque_part); }
		if (parseduri.hierpart)
		{
			if (parseduri.hierpart->abspathsegs)
			{
				int i=0;
				while (*((parseduri.hierpart->abspathsegs)+i)!=NULL)
				{
					printf("path segment %d:",i+1);
					puts(*((parseduri.hierpart->abspathsegs)+i));
					i++;
				}
			}
		}
	}
	else
	{
		printf("URI was not parsed.\n");
	}

	freeparseduri(&parseduri);
	
	return (0);
}
 
Old 04-17-2004, 10:10 PM   #5
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
no takers i see. well Im going to bed.


hopefully my brain will figure out the problem while im asleep

Nighty nite!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
memory allocation esael Linux - General 7 01-12-2008 12:12 PM
Linux memory allocation to programs mlaudu Linux - Software 1 03-29-2004 04:25 PM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:24 PM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:22 PM
memory allocation. raven Programming 5 09-08-2002 01:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration