Thread: Trouble understanding C memalign

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    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.

  2. #2
    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;  
    }

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