Thread: Trouble understanding C memalign

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    106

    Trouble understanding C memalign

    Hi Guys,
    I’m trying to port a file browser from one platform to another, and trying to avoid using any new C functions if possible.

    I’m reading this link:
    The GNU C Library: Aligned Memory Blocks

    and still having trouble with what memlalign does.
    I understand memset, and think that i can easily replace that with code of my own.

    With memset, I don’t understand what’s called a boundary in that link,
    and it would be appreciated if someone could explain what it does that I couldn’t do by declaring my own array.

    What I suspect, but still don’t know is the block of memory can all be addressed from an offset by a small variable.
    Cheers, Art.
    Last edited by xArt; 09-20-2016 at 08:09 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You shouldn't normally need to call memalign (or it's aliases), because the compiler will align all the in-built data types on the correct memory boundary for you.

    I suppose the first thing to do is understand why memalign is being called. Is it for a good reason, or just to give the original programmer a "warm and fuzzy" feeling.

    Good reasons would be
    - the buffer is being passed to the OS for use in some DMA transfer
    - the buffer will contain wide alignment data types, say for example the load/store of MMX registers.

    When you call an aligned memory function, the alignment is saying how many of the LSBits of the address will be zero.
    Eg.
    void *p = memalign ( 16, 256 );
    In binary, p will be xxxx-xxxx-xxxx-0000
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    Quote Originally Posted by Salem View Post
    - the buffer is being passed to the OS for use in some DMA transfer
    It’s being sent to an API for file IO for the intended platform which was Sony PSP, and have similar file IO functions for other platforms.
    I can’t pretend to follow it like execution, but understand that it looks for a particular directory (Music) and iteratively populates a structure with number and file,
    and can also test an entry is a file or another directory, to allow stepping into the new directory and caching that one.

    If I understand you correct you could save memory by having it squash the array?

    Although this calls Sony functions (Sony Computer Entertainment prefix), it’s part of a free home-brew SDK, so assume it’s ok to post.

    Code:
    void recacheGameDir() { 
    			struct SceIoDirent dir; 
    			memset( &dir, 0, sizeof(SceIoDirent)); 
    			static int dfd; 
    
    
    			if (musicfolderexists == 0) {
    			dfd = sceIoDopen(current_dir);
    			} else {
    			dfd = sceIoDopen(current_dirx);
    			}
    
    
    			if(dfd > 0) { 
    			int f = 0;
    			for( f = 0; f < MAX_ENTRIES; f++) {
    			if( gameEntry[f].name) {
    			free( gameEntry[f].name);
    			}
    			gameEntry[f].name = NULL;
    			}
    			countentry = 0;
    			while( sceIoDread( dfd, &dir) > 0) { 
    			static char * name; 
    			name = (char*)memalign( 16, 300);
    			sprintf( name,"%s" , dir.d_name); 
    			static int s = 0; 
    			s = strlen(name); 
    			gameEntry[countentry].name = name; 
    			countentry++;
    			if(countentry > MAX_ENTRIES - 1) {
    			countentry = MAX_ENTRIES - 1;
    			} 
    			gameEntriesTotal = countentry;
    			if(gameEntriesTotal < 0) {
    			gameEntriesTotal = 0;
    			} 
    			} 
    			} 
    			sceIoDclose(dfd); 
    } 
    
    
    int is_file (const char *filename) { 
    			SceIoStat stats;
    			sceIoGetstat( filename, &stats);
    			if ( stats.st_mode & FIO_S_IFDIR) 
    			return 0; 
    			else 
    			return 1;  
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I can't see anything there that would warrant memalign (even through the awful formatting).

    I see no reason for any of the variables to be static.

    Using sprintf to do a strcpy() is a waste of effort.

    Using the result of strlen() in the allocation call would be good, instead of assuming some magic number constant. If you really wanted to save memory, then only allocating what you need (instead of average worst guess) would be the way to go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    It will be some time before I can try. I just came back to thank you for the replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble understanding constructor
    By seal308 in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2016, 05:41 PM
  2. Trouble understanding some code please help
    By Shinzu911 in forum C Programming
    Replies: 1
    Last Post: 02-19-2013, 09:01 PM
  3. Trouble Understanding the assignment given.
    By AlbyRang in forum C Programming
    Replies: 3
    Last Post: 03-30-2012, 01:51 AM
  4. a little trouble understanding
    By zema in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2003, 05:49 PM
  5. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 09:00 AM

Tags for this Thread