473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

weird but interesting code - needs help

vib
Hi there,

I am looking at this code for quite awhile, still don't any clue why
one wants to do it this way. It is about display message through UART
or COM port. Here is the code.

The calling code:
[1] MyDbgPrintf(("File Write Error\n"));

The called code:
[2] #define MyDbgPrintf(x) UART_Printf x

MOre, the body of UART_Printf:

[3]
void UART_Printf( const char *fmt, ... )
{
v_list ap;
char string[1024];

if( gUart_init == TRUE )
{
v_start( ap, fmt );
vsprintf( string, fmt, ap );
UART_SendString( string );
v_end( ap );
}
}

And the
[3]
void UART_SendString(char *string )
{
if( gUart_init == TRUE )
{
while( *string )
UART_SendByte( *string++ );
}
}

[4]
typedef int *v_list[1];

[5]
#define v_start(ap, parmN) (void)(*(ap) = __va_start(parmN))
[6]
#define v_end(ap) ((void)(*(ap) = 0))
Any comments?

what are [4], [5] [6] and why?

Thanks in advance.

Nov 15 '05 #1
4 1539
In article <11*********************@f14g2000cwb.googlegroups. com>,
vib <vi*****@gmail.com> wrote:
I am looking at this code for quite awhile, still don't any clue why
one wants to do it this way. It is about display message through UART
or COM port. v_list ap; v_start( ap, fmt );
vsprintf( string, fmt, ap );
UART_SendString( string );
v_end( ap ); [4]
typedef int *v_list[1]; [5]
#define v_start(ap, parmN) (void)(*(ap) = __va_start(parmN)) [6]
#define v_end(ap) ((void)(*(ap) = 0)) Any comments? what are [4], [5] [6] and why?

I suggest that you read the documentation about the elipsis
(...) prototype and the mechanisms by which one accesses variable
number of arguments. The relevant man page on unix-type systems
is often named stdarg

[4], [5], and [6] are all internal workings of a mechanism that
you should not be poking around in unless you are trying to
implement a C compiler. There are different expansions for them
on different systems.
By the way, the program has a potential buffer overflow problem.
If UART_Printf() is handed a parameter list that expands to more
than 1023 characters, the program will likely corrupt memory.
--
Programming is what happens while you're busy making other plans.
Nov 15 '05 #2
vib
Any benefits of using this method? Just thinking using a simple, easy
understand method, like writting to a circular buffer, and the TX empty
interrupt may read the circular buffer and send it to the UART. Simple
yet easy to implement.

Nov 15 '05 #3
In article <11*********************@f14g2000cwb.googlegroups. com>,
vib <vi*****@gmail.com> wrote:
Any benefits of using this method?
Which method? You did not quote any context, and I've read approximately
118 postings and 1063 email messages since your previous message.
Just thinking using a simple, easy
understand method, like writting to a circular buffer, and the TX empty
interrupt may read the circular buffer and send it to the UART. Simple
yet easy to implement.


UART... TX buffer... lemme rummage through my memory.

The layer you are neglecting is the -formatting- of the data to
write to the buffer. That's what the code you pointed to before
takes care of. It isn't until about 3 layers down that it gets to
the transmission of bytes to the UART, and it does that by way of
the UART_Send routine that we aren't shown the implementation of.
Whether the UART_Send works on a circular buffer or something else
is transparent to this level of code.

You shouldn't be looking at the va_* code as having anything to do
with the complicating of the routine. It -is- a "simple, easy understand"
method at the upper level. Your code just calls the debug
printf layer and passes whatever format string and argument that
it needs, and everything else is automagically taken care of.
You only need to get down to the lower level if you are the person
who has to implement the circular buffer or whatever...

If you -are- the person who needs to implement a circular buffer,
then consider exactly what the user interface would be. If the
circular buffer is partly full at the moment, and I ask to add
something to it, then suppose what I'm adding is too big to fit
into the buffer all at one time. What is your routine going to do then?
Reject the entire string that I want to add? Add what it can
and return the count (or buffer pointer) to the rest? Add what
it can at the moment, sleep a bit and add some more, and so on until
the entire message is inserted? One of the simplest insertion
mechanisms is to check to see if there is room for a byte,
add it if so, and if not then wait for a signal from the writing
routine saying it pulled something out of the buffer (or for
a sanity alarm in case that signal got lost somehow.) And that's
effectively the interface that this code has presented, with it's
preperation of the output string first and then looping sending each
byte in sequence using some unspecified transmission mechanism.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 15 '05 #4
vib
Thanks for the lengthy explanation, really appreciate that.

Walter Roberson wrote:
In article <11*********************@f14g2000cwb.googlegroups. com>,
vib <vi*****@gmail.com> wrote:
Any benefits of using this method?


Which method? You did not quote any context, and I've read approximately
118 postings and 1063 email messages since your previous message.
Just thinking using a simple, easy
understand method, like writting to a circular buffer, and the TX empty
interrupt may read the circular buffer and send it to the UART. Simple
yet easy to implement.


UART... TX buffer... lemme rummage through my memory.

The layer you are neglecting is the -formatting- of the data to
write to the buffer. That's what the code you pointed to before
takes care of. It isn't until about 3 layers down that it gets to
the transmission of bytes to the UART, and it does that by way of
the UART_Send routine that we aren't shown the implementation of.
Whether the UART_Send works on a circular buffer or something else
is transparent to this level of code.

You shouldn't be looking at the va_* code as having anything to do
with the complicating of the routine. It -is- a "simple, easy understand"
method at the upper level. Your code just calls the debug
printf layer and passes whatever format string and argument that
it needs, and everything else is automagically taken care of.
You only need to get down to the lower level if you are the person
who has to implement the circular buffer or whatever...

If you -are- the person who needs to implement a circular buffer,
then consider exactly what the user interface would be. If the
circular buffer is partly full at the moment, and I ask to add
something to it, then suppose what I'm adding is too big to fit
into the buffer all at one time. What is your routine going to do then?
Reject the entire string that I want to add? Add what it can
and return the count (or buffer pointer) to the rest? Add what
it can at the moment, sleep a bit and add some more, and so on until
the entire message is inserted? One of the simplest insertion
mechanisms is to check to see if there is room for a byte,
add it if so, and if not then wait for a signal from the writing
routine saying it pulled something out of the buffer (or for
a sanity alarm in case that signal got lost somehow.) And that's
effectively the interface that this code has presented, with it's
preperation of the output string first and then looping sending each
byte in sequence using some unspecified transmission mechanism.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson


Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jim Dawson | last post by:
I was writing a subroutine to extract fields from lines of text when I ran into an issue. I have reproduced this error on Perl 5.8 on AIX, 5.8 on Linux and 5.6 on Windows. ############### CUT...
2
by: jwbeaty | last post by:
Here's a weird one. I'm running SQL Server 7 and when I run a backup something weird happens. When I perform the backup via Enterprise Manager by right clicking on the database I want to...
0
by: Zwyatt | last post by:
having a really weird little bug w/ time_t...check it out: I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum;
10
by: Don Munroe | last post by:
This one has me stumped. I have three web applications running on two different servers. The first that works fine is hosted by a .Net hosting company. Everyone that uses it has no problems...
41
by: Petr Jakes | last post by:
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for...
4
by: sparks | last post by:
If Not IsNull(DLookup(, "tblDemographic", " = """ & Me.Text9 & """")) Then Cancel = True MsgBox "Duplicate Value is Not Allowed" ActiveControl.Undo DoCmd.RunCommand acCmdUndo End If I try to...
1
by: PeaceManGroove | last post by:
It's a long story, but our application needs to run in Access 97. All of the computers in our organisation also run Access 2003. . . C++ code launches our Access reports via a VB 6.0 program. ...
6
by: Lucas Kanebley Tavares | last post by:
Hello all, I have a templatized class which has an attribute as: "T *data", all constructors initialize it to zero, and then allocate memory for the array (and that IS done correctly, I've...
6
by: Emiurgo | last post by:
Hi there to everyone! I've got a problem which may be interesting to some of you, and I'd be very grateful if there is someone who can give me some advice (or maybe redirect me to some other place)....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.