code never executed

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys, I have a function and I set a value, "boundary_violation", but later when I test it, it never executes?  Have a look at the snapshot. 

	// locate number of consecutive regions in cluster
	for(int consecutive_regions = 0; consecutive_regions < transactionSize; DirectoryExt++){

		/** Boundary Reached, load next cluster */
		if(((char *)DirectoryExt - file->directory_base) > cluster_size(file)){


			boundary_violation = 0xffffffff;

			dir_load_next_cluster(file);

			printf("\n\rViolation Marker!!!");

			DirectoryExt = (struct directory_entry * )file->directory_base;
		}


		// free location segment found
		if((DirectoryExt->entry_type == 0x00) ||
		   (DirectoryExt->entry_type == 0x40) ||
		   (DirectoryExt->entry_type == 0x41) ||
		   (DirectoryExt->entry_type == 0x05)){

			// mark free location
			consecutive_regions++;

			// log first entry
			if(consecutive_regions == 1)
				DirectoryExtTarget = DirectoryExt;
		}
		else{
			consecutive_regions = 0;
		}
	}


	// if entry spans clusters then save new boundary and go to the old cluster
	if(boundary_violation != 0){

		printf("\n\rBoundary Detected!!!");

		// save new cluster details
		temp1 = file->cluster_nr;
		temp2 = file->cluster_addr;

		// load old cluster
		file->cluster_nr = old_cluster_nr;
		file->cluster_addr = old_address;

		// update data cluster information
		dma_hardware_read(1, old_address, sectors_per_cluster(file), file->directory_base, 0);
	}

The following part never executes when "boundary_violation" is set.

if(boundary_violation != 0){

		printf("\n\rBoundary Detected!!!");

		// save new cluster details
		temp1 = file->cluster_nr;
		temp2 = file->cluster_addr;

		// load old cluster
		file->cluster_nr = old_cluster_nr;
		file->cluster_addr = old_address;

		// update data cluster information
		dma_hardware_read(1, old_address, sectors_per_cluster(file), file->directory_base, 0);
	}

Help!!!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not enough context

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's definitely being set in the function, it does the "printf("\n\rViolation Marker!!!");".  Its set no where else but still does not enter "if(boundary_violation != 0){"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

print something ( "Hello, I've arrived in my Ferrari"), right before 

if(boundary_violation != 0){

...maybe you never even make it to this point  (gets lost in the weeds along the way)

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. Jul 1, 2019 - 06:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

changed my mind!

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Mon. Jul 1, 2019 - 06:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi lads, got it working, can I ask, "Clawson" when I get a new cluster should I memset its contents to '0'?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can only assume they are differently scoped boundary_violation  or that somewhere else it is zeroed.

 

This is on ARM right ? Surely you have a debugger.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What ended up causing the problem?

 

Some thoughts:

  • From the minimal code you posted it looks like you are just using boundary_violation as a boolean. If this is the case boolean semantics would increase readability.
  • What in the world is the significance of the entry_type magic numbers?
  • Since you didn't post the whole function I'm guessing this function is quite long which makes reasoning about your code more difficult than it needs to be. The amount of branching that is going on doesn't help either.
  • Poor locality of reference and non-descriptive names increase the difficulty of understanding what is going on (are you able to look at uses of temp1/temp2 and understand what is going on without scrolling back).
  • The type casts seem suspicious and may indicate the presence of a design flaw.

github.com/apcountryman/build-avr-gcc: a script for building avr-gcc

github.com/apcountryman/toolchain-avr-gcc: a CMake toolchain for cross compiling for the Atmel AVR family of microcontrollers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No need for memset(). Anything beyond the last fwrite() will never be returned in an fread() anyway. I guess you could do it simply for tidiness.  But anyway while a cluster may be 16KB or 32KB or something you aren't going to cache the entire thing and you'll only write a sector at a time surely?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you are concerned about privacy/security, you can

memset allocated memory (RAM, disk, etc.) so the

previous contents are not readable.  Or when you

free a string or a disk block you can overwrite the

contents.

 

Just make sure the code is not optimized away by

the compiler (since it might decide not to memset

anything if you subsequently free the memory).

 

--Mike

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

No need for memset(). Anything beyond the last fwrite() will never be returned in an fread() anyway. I guess you could do it simply for tidiness.  But anyway while a cluster may be 16KB or 32KB or something you aren't going to cache the entire thing and you'll only write a sector at a time surely?

 

Hi Clawson, I was thinking of just working with sectors (512Bytes) instead of clusters for the reason you gave.  But then again I have loads of RAM.  Do you think I should cache sectors only?

 

I'm guessing that working with sectors would be easier and more portable than clusters

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Caching sectors seems more sensible to me. Caching clusters works against you with small files (ie file length < cluster size or modulus thereof). If the user wants to cache their files, they can do that in their application. Also, whilst you have tons of ram, it still is a finite resource. Also as you increase the size of your cache the net improvement decreases.ie double the cache wont necessarily double the net gain.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Caching sectors seems more sensible to me. Caching clusters works against you with small files (ie file length < cluster size or modulus thereof). If the user wants to cache their files, they can do that in their application. Also, whilst you have tons of ram, it still is a finite resource. Also as you increase the size of your cache the net improvement decreases.ie double the cache wont necessarily double the net gain.

 

Agree Kartman.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you'll find that "big operating systems" like Windows/Linux probably actually cache multiple clusters not just a handful of sectors. Of course there's a tradeoff. You might choose when a file is opened to cache the first 16 clusters in the chain (say) just in case the user goes on to read the data sequentially but if they now actually do an fseek(SEEK_END) or something you may have wasted time/effort pre-reading data. But caches are a bit like this - you need to tune operation based on expected behaviour.

 

FatFs only ever has a one 512 byte sector cache !