Forced to define Go struct for casting an unsafe.Pointer() to a C struct - c

Interoperating with C code, I was not able to directly cast a structure and I was forced to define an equivalent one in Go.
The C function from libproc.h is
int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize)
The C structure for flavor==PROC_PIDTASKINFO is proc_taskinfo as defined in sys/proc_info.h (included by libproc.h):
struct proc_taskinfo {
uint64_t pti_virtual_size; /* virtual memory size (bytes) */
uint64_t pti_resident_size; /* resident memory size (bytes) */
uint64_t pti_total_user; /* total time */
uint64_t pti_total_system;
uint64_t pti_threads_user; /* existing threads only */
uint64_t pti_threads_system;
int32_t pti_policy; /* default policy for new threads */
int32_t pti_faults; /* number of page faults */
int32_t pti_pageins; /* number of actual pageins */
int32_t pti_cow_faults; /* number of copy-on-write faults */
int32_t pti_messages_sent; /* number of messages sent */
int32_t pti_messages_received; /* number of messages received */
int32_t pti_syscalls_mach; /* number of mach system calls */
int32_t pti_syscalls_unix; /* number of unix system calls */
int32_t pti_csw; /* number of context switches */
int32_t pti_threadnum; /* number of threads in the task */
int32_t pti_numrunning; /* number of running threads */
int32_t pti_priority; /* task priority*/
};
Even if Go code actually works, I was not able to use C.proc_taskinfo directly. The Go function is propertiesOf(): complete source here.
If I reference the C structure, I got a similar error reported as in my latest question on the subject: could not determine kind of name for C.proc_taskinfo, but this time I'm sure the definition is imported with #include.

As per documentation
To access a struct, union, or enum type directly, prefix it with struct_, union_, or enum_, as in C.struct_stat.
Use C.struct_proc_taskinfo.

Related

What kinds of data in a device driver can be shared among processes?

In device drivers, how can we tell what data is shared among processes and what is local to a process? The Linux Device Drivers book mentions
Any time that a hardware or software resource is shared beyond a single thread of execution, and the possibility exists that one thread could encounter an inconsistent view of that resource, you must explicitly manage access to that resource.
But what kinds of software resources can be shared among threads and what kinds of data cannot be shared? I know that global variables are generally considered as shared memory but what other kinds of things need to be protected?
For example, is the struct inode and struct file types passed in file operations like open, release, read, write, etc. considered to be shared?
In the open call inside main.c , why is dev (in the line dev = container_of(inode->i_cdev, struct scull_dev, cdev);) not protected with a lock if it points to a struct scull_dev entry in the global array scull_devices?
In scull_write, why isn't the line int quantum = dev->quantum, qset = dev->qset; locked with a semaphore since it's accessing a global variable?
/* In scull.h */
struct scull_qset {
void **data; /* pointer to an array of pointers which each point to a quantum buffer */
struct scull_qset *next;
};
struct scull_dev {
struct scull_qset *data; /* Pointer to first quantum set */
int quantum; /* the current quantum size */
int qset; /* the current array size */
unsigned long size; /* amount of data stored here */
unsigned int access_key; /* used by sculluid and scullpriv */
struct semaphore sem; /* mutual exclusion semaphore */
struct cdev cdev; /* Char device structure */
};
/* In main.c */
struct scull_dev *scull_devices; /* allocated in scull_init_module */
int scull_major = SCULL_MAJOR;
int scull_minor = 0;
int scull_nr_devs = SCULL_NR_DEVS;
int scull_quantum = SCULL_QUANTUM;
int scull_qset = SCULL_QSET;
ssize_t scull_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
struct scull_dev *dev = filp->private_data; /* flip->private_data assigned in scull_open */
struct scull_qset *dptr;
int quantum = dev->quantum, qset = dev->qset;
int itemsize = quantum * qset;
int item; /* item in linked list */
int s_pos; /* position in qset data array */
int q_pos; /* position in quantum */
int rest;
ssize_t retval = -ENOMEM; /* value used in "goto out" statements */
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
/* find listitem, qset index and offset in the quantum */
item = (long)*f_pos / itemsize;
rest = (long)*f_pos % itemsize;
s_pos = rest / quantum;
q_pos = rest % quantum;
/* follow the list up to the right position */
dptr = scull_follow(dev, item);
if (dptr == NULL)
goto out;
if (!dptr->data) {
dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
if (!dptr->data)
goto out;
memset(dptr->data, 0, qset * sizeof(char *));
}
if (!dptr->data[s_pos]) {
dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
if (!dptr->data[s_pos])
goto out;
}
/* write only up to the end of this quantum */
if (count > quantum - q_pos)
count = quantum - q_pos;
if (copy_from_user(dptr->data[s_pos]+q_pos, buf, count)) {
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
/* update the size */
if (dev->size < *f_pos)
dev->size = *f_pos;
out:
up(&dev->sem);
return retval;
}
int scull_open(struct inode *inode, struct file *filp)
{
struct scull_dev *dev; /* device information */
/* Question: Why was the lock not placed here? */
dev = container_of(inode->i_cdev, struct scull_dev, cdev);
filp->private_data = dev; /* for other methods */
/* now trim to 0 the length of the device if open was write-only */
if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) {
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
scull_trim(dev); /* ignore errors */
up(&dev->sem);
}
return 0; /* success */
}
int scull_init_module(void)
{
int result, i;
dev_t dev = 0;
/* assigns major and minor numbers (left out for brevity) */
/*
* allocate the devices -- we can't have them static, as the number
* can be specified at load time
*/
scull_devices = kmalloc(scull_nr_devs * sizeof(struct scull_dev), GFP_KERNEL);
if (!scull_devices) {
result = -ENOMEM;
goto fail; /* isn't this redundant? */
}
memset(scull_devices, 0, scull_nr_devs * sizeof(struct scull_dev));
/* Initialize each device. */
for (i = 0; i < scull_nr_devs; i++) {
scull_devices[i].quantum = scull_quantum;
scull_devices[i].qset = scull_qset;
init_MUTEX(&scull_devices[i].sem);
scull_setup_cdev(&scull_devices[i], i);
}
/* some other stuff (left out for brevity) */
return 0; /* succeed */
fail:
scull_cleanup_module(); /* left out for brevity */
return result;
}
/*
* Set up the char_dev structure for this device.
*/
static void scull_setup_cdev(struct scull_dev *dev, int index)
{
int err, devno = MKDEV(scull_major, scull_minor + index);
cdev_init(&dev->cdev, &scull_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &scull_fops; /* isn't this redundant? */
err = cdev_add (&dev->cdev, devno, 1);
/* Fail gracefully if need be */
if (err)
printk(KERN_NOTICE "Error %d adding scull%d", err, index);
}
All data in memory can be considered a "shared resource" if both threads are able to access it*. The only resource they wouldn't be shared between processors is the data in the registers, which is abstracted away in C.
There are two reasons that you would not practically consider two resources to be shared (even though they do not actually mean that two threads could not theoretically access them, some nightmarish code could sometimes bypass these).
Only one thread can/does access it. Clearly if only one thread accesses a variable then there can be no race conditions. This is the reason local variables and single threaded programs do not need locking mechanisms.
The value is constant. You can't get different results based on order of access if the value can never change.
The program you have shown here is incomplete, so it is hard to say, but each of the variables accessed without locking must meet one of the criteria for this program to be thread safe.
There are some non-obvious ways to meet the criteria, such as if a variable is constant or limited to one thread only in a specific context.
You gave two examples of lines that were not locked. For the first line.
dev = container_of(inode->i_cdev, struct scull_dev, cdev);
This line does not actually access any variables, it just computes where the struct containing cdev would be. There can be no race conditions because nobody else has access to your pointers (though they have access to what they point to), they are only accessible within the function (this is not true of what they point to). This meets criteria (1).
The other example is
int quantum = dev->quantum, qset = dev->qset;
This one is a bit harder to say without context, but my best guess is that it is assumed that dev->quantum and dev->qset will never change during the function call. This seems supported by the fact that they are only called in scull_init_module which should only be called once at the very beginning. I believe this fits criteria (2).
Which brings up another way that you might change a shared variable without locking, if you know that other threads will not try to access it until you are done for some other reason (eg they are not extant yet)
In short, all memory is shared, but sometimes you can get away with acting like its not.
*There are embedded systems where each processor has some amount of RAM that only it could use, but this is not the typical case.

How to get the message length using ISO8583 library by Oscar Anderson?

How can i geth the length of my isomsg using Oscar Anderson's C library
Supposed i have this:
enter code here
DL_ISO8583_HANDLER isoHandler;
DL_ISO8583_MSG isoMsg;
DL_UINT8 buf[MAX_DATASIZ];
DL_UINT16 bufSize;
DL_ISO8583_DEFS_1987_GetHandler(&isoHandler);
/* initialise ISO message */
DL_ISO8583_MSG_Init(NULL,0,&isoMsg);
(void)DL_ISO8583_MSG_SetField_Str(0,"0320",&isoMsg);
(void)DL_ISO8583_MSG_SetField_Str(2,"4401410145200145",&isoMsg);
(void)DL_ISO8583_MSG_SetField_Str(3,"310000",&isoMsg);
(void)DL_ISO8583_MSG_SetField_Str(4,"000000001045",&isoMsg);
(void)DL_ISO8583_MSG_SetField_Str(11,"000015",&isoMsg);
(void)DL_ISO8583_MSG_Pack(&isoHandler,&isoMsg,&buf[7],&bufSize);
How can get my isoMsg length?
from what I see here is the definition of DL_ISO8583_MSG:
struct DL_ISO8583_MSG_S
{
/* static memory details */
DL_UINT8 *sPtrNext; /* next static point - NULL if dynamic mode */
DL_UINT8 *sPtrEnd; /* end of the static buffer (if static mode) */
/* NB bitmap is not stored, it is implied if the field is set */
/* fields */
DL_ISO8583_MSG_FIELD field[1+kDL_ISO8583_MAX_FIELD_IDX];
};
typedef struct DL_ISO8583_MSG_S DL_ISO8583_MSG;
just parse through the DL_UINT8 pointers and store the count into a variable.

How To Understand Mach-O Symbol Table

I am currently learning how to disassemble Mach-O Binary Files and I am trying to figure out how to understand a 'Symbol Table' (in load cmd LC_SYMTAB).
How Do I Read / Interpret A Symbol Table and its entries?
I am not 100% of this but it appears that the entries are 8 bytes each? (correct me if I'm wrong)
I know that a string table is a group of strings separated by null bytes but what is a Symbol Table and its purpose?
Thanks.
Straight from <mach-o/nlist.h>:
struct nlist {
union {
uint32_t n_strx; /* index into the string table */
} n_un;
uint8_t n_type; /* type flag, see below */
uint8_t n_sect; /* section number or NO_SECT */
int16_t n_desc; /* see <mach-o/stab.h> */
uint32_t n_value; /* value of this symbol (or stab offset) */
};
struct nlist_64 {
union {
uint32_t n_strx; /* index into the string table */
} n_un;
uint8_t n_type; /* type flag, see below */
uint8_t n_sect; /* section number or NO_SECT */
uint16_t n_desc; /* see <mach-o/stab.h> */
uint64_t n_value; /* value of this symbol (or stab offset) */
};
So no, that shouldn't be 8 bytes, but rather 12 bytes for 32-bit and 16 bytes for 64-bit binaries.

"error: unknown type name" after declaration

i'm developing a encoder/decoder program, and I used the asn1c compiler to covert my ASN.1 code to C. When you do that, specific .c and .h files are automatically added such as type declarations as well as encoding and decoding files. When I put all these together I keep getting
/Desktop/asn1c/constr_TYPE.h:97:2: error: unknown type name ‘der_type_encoder_f’
however inside one of these files they type is explicitly defined. below I have posted the main encoding program, the file I believe is causing the problem, as well as my Makefile.
this is the main program.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include "/Desktop/asn1c/BACnet-SimpleACK-PDU.h"
#include "/Desktop/asn1c/asn_application.h"
#include "/Desktop/asn1c/constr_TYPE.h"
#include "/Desktop/asn1c/der_encoder.h"
static int write_out(const void *buffer, size_t size, void *app_key){
FILE *out_fp = app_key;
size_t wrote = fwrite(buffer, 1, size, out_fp);
return (wrote == size) ? 0 : -1;
}
int main(int ac, char **av){
BACnet_SimpleACK_PDU_t *bacnet_simpleack_pdu;
asn_enc_rval_t ec;
bacnet_simpleack_pdu = calloc(1, sizeof(BACnet_SimpleACK_PDU_t));
bacnet_simpleack_pdu -> pdu_type = 15;
bacnet_simpleack_pdu -> reserved = 0;
bacnet_simpleack_pdu -> invokeID = 45;
bacnet_simpleack_pdu -> service_ACK_choice = 0;
const char *filename = av[1];
FILE *fp;
fp = fopen(filename, "wb");
ec = der_encode(&asn_DEF_BACnet_SimpleACK_PDU, bacnet_simpleack_pdu, write_out, fp);
fclose(fp);
}
The Next file below is the one causing the problem
#ifndef _CONSTR_TYPE_H_
#define _CONSTR_TYPE_H_
#include "ber_tlv_length.h"
#include "ber_tlv_tag.h"
#ifdef __cplusplus
extern "C" {
#endif
struct asn_TYPE_descriptor_s; /* Forward declaration */
struct asn_TYPE_member_s; /* Forward declaration */
/*
* This type provides the context information for various ASN.1 routines,
* primarily ones doing decoding. A member _asn_ctx of this type must be
* included into certain target language's structures, such as compound types.
*/
typedef struct asn_struct_ctx_s {
short phase; /* Decoding phase */
short step; /* Elementary step of a phase */
int context; /* Other context information */
void *ptr; /* Decoder-specific stuff (stack elements) */
ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */
} asn_struct_ctx_t;
#include "ber_decoder.h" /* Basic Encoding Rules decoder */
#include "der_encoder.h" /* Distinguished Encoding Rules encoder */
#include "xer_decoder.h" /* Decoder of XER (XML, text) */
#include "xer_encoder.h" /* Encoder into XER (XML, text) */
#include "per_decoder.h" /* Packet Encoding Rules decoder */
#include "per_encoder.h" /* Packet Encoding Rules encoder */
#include "constraints.h" /* Subtype constraints support */
/*
* Free the structure according to its specification.
* If (free_contents_only) is set, the wrapper structure itself (struct_ptr)
* will not be freed. (It may be useful in case the structure is allocated
* statically or arranged on the stack, yet its elements are allocated
* dynamically.)
*/
typedef void (asn_struct_free_f)(
struct asn_TYPE_descriptor_s *type_descriptor,
void *struct_ptr, int free_contents_only);
#define ASN_STRUCT_FREE(asn_DEF, ptr) (asn_DEF).free_struct(&(asn_DEF),ptr,0)
#define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \
(asn_DEF).free_struct(&(asn_DEF),ptr,1)
short phase; /* Decoding phase */
short step; /* Elementary step of a phase */
int context; /* Other context information */
void *ptr; /* Decoder-specific stuff (stack elements) */
ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */
} asn_struct_ctx_t;
#include "ber_decoder.h" /* Basic Encoding Rules decoder */
#include "der_encoder.h" /* Distinguished Encoding Rules encoder */
#include "xer_decoder.h" /* Decoder of XER (XML, text) */
#include "xer_encoder.h" /* Encoder into XER (XML, text) */
#include "per_decoder.h" /* Packet Encoding Rules decoder */
#include "per_encoder.h" /* Packet Encoding Rules encoder */
#include "constraints.h" /* Subtype constraints support */
/*
* Free the structure according to its specification.
* If (free_contents_only) is set, the wrapper structure itself (struct_ptr)
* will not be freed. (It may be useful in case the structure is allocated
* statically or arranged on the stack, yet its elements are allocated
* dynamically.)
*/
typedef void (asn_struct_free_f)(
struct asn_TYPE_descriptor_s *type_descriptor,
void *struct_ptr, int free_contents_only);
#define ASN_STRUCT_FREE(asn_DEF, ptr) (asn_DEF).free_struct(&(asn_DEF),ptr,0)
#define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \
(asn_DEF).free_struct(&(asn_DEF),ptr,1)
/*
* Print the structure according to its specification.
*/
typedef int (asn_struct_print_f)(
struct asn_TYPE_descriptor_s *type_descriptor,
const void *struct_ptr,
int level, /* Indentation level */
asn_app_consume_bytes_f *callback, void *app_key);
/*
* Return the outmost tag of the type.
* If the type is untagged CHOICE, the dynamic operation is performed.
* NOTE: This function pointer type is only useful internally.
* Do not use it in your application.
*/
typedef ber_tlv_tag_t (asn_outmost_tag_f)(
struct asn_TYPE_descriptor_s *type_descriptor,
const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);
/* The instance of the above function type; used internally. */
asn_outmost_tag_f asn_TYPE_outmost_tag;
^L
/*
* The definitive description of the destination language's structure.
*/
typedef struct asn_TYPE_descriptor_s {
char *name; /* A name of the ASN.1 type. "" in some cases. */
char *xml_tag; /* Name used in XML tag */
/*
* Generalized functions for dealing with the specific type.
* May be directly invoked by applications.
*/
asn_struct_free_f *free_struct; /* Free the structure */
asn_struct_print_f *print_struct; /* Human readable output */
asn_constr_check_f *check_constraints; /* Constraints validator */
ber_type_decoder_f *ber_decoder; /* Generic BER decoder */
der_type_encoder_f *der_encoder; /* Canonical DER encoder */
xer_type_decoder_f *xer_decoder; /* Generic XER decoder */
xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */
per_type_decoder_f *uper_decoder; /* Unaligned PER decoder */
per_type_encoder_f *uper_encoder; /* Unaligned PER encoder */
/***********************************************************************
* Internally useful members. Not to be used by applications directly. *
**********************************************************************/
/*
* Tags that are expected to occur.
*/
asn_outmost_tag_f *outmost_tag; /* <optional, internal> */
ber_tlv_tag_t *tags; /* Effective tags sequence for this type */
int tags_count; /* Number of tags which are expected */
ber_tlv_tag_t *all_tags;/* Every tag for BER/containment */
int all_tags_count; /* Number of tags */
asn_per_constraints_t *per_constraints; /* PER compiled constraints */
/*
* An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).
*/
struct asn_TYPE_member_s *elements;
int elements_count;
/*
* Additional information describing the type, used by appropriate
* functions above.
*/
void *specifics;
} asn_TYPE_descriptor_t;
/*
* This type describes an element of the constructed type,
* i.e. SEQUENCE, SET, CHOICE, etc.
*/
enum asn_TYPE_flags_e {
ATF_NOFLAGS,
ATF_POINTER = 0x01, /* Represented by the pointer */
ATF_OPEN_TYPE = 0x02 /* ANY type, without meaningful tag */
};
typedef struct asn_TYPE_member_s {
enum asn_TYPE_flags_e flags; /* Element's presentation flags */
int optional; /* Following optional members, including current */
int memb_offset; /* Offset of the element */
ber_tlv_tag_t tag; /* Outmost (most immediate) tag */
int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */
asn_TYPE_descriptor_t *type; /* Member type descriptor */
asn_constr_check_f *memb_constraints; /* Constraints validator */
asn_per_constraints_t *per_constraints; /* PER compiled constraints */
int (*default_value)(int setval, void **sptr); /* DEFAULT <value> */
char *name; /* ASN.1 identifier of the element */
} asn_TYPE_member_t;
/*
* BER tag to element number mapping.
*/
typedef struct asn_TYPE_tag2member_s {
ber_tlv_tag_t el_tag; /* Outmost tag of the member */
int el_no; /* Index of the associated member, base 0 */
int toff_first; /* First occurence of the el_tag, relative */
int toff_last; /* Last occurence of the el_tag, relatvie */
} asn_TYPE_tag2member_t;
/*
* This function is a wrapper around (td)->print_struct, which prints out
* the contents of the target language's structure (struct_ptr) into the
* file pointer (stream) in human readable form.
* RETURN VALUES:
* 0: The structure is printed.
* -1: Problem dumping the structure.
* (See also xer_fprint() in xer_encoder.h)
*/
int asn_fprint(FILE *stream, /* Destination stream descriptor */
asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */
const void *struct_ptr); /* Structure to be printed */
#ifdef __cplusplus
}
#endif
Here is the Makefile
ecoder_test: ecoder_test.c /Desktop/asn1c/BACnet-SimpleACK-PDU.h /Desktop/asn1c/OSUINT.h /Desktop/asn1c/BACnetConfirmedServiceChoice.h /Desktop/asn1c/constr_SEQUENCE.h /Desktop/asn1c/constr_TYPE.h /Desktop/asn1c/asn_application.h /Desktop/asn1c/der_encoder.h /Desktop/asn1c/ber_tlv_length.h
gcc ecoder_test.c /Desktop/asn1c/BACnet-SimpleACK-PDU.h /Desktop/asn1c/OSUINT.h /Desktop/asn1c/BACnetConfirmedServiceChoice.h /Desktop/asn1c/constr_SEQUENCE.h /Desktop/asn1c/constr_TYPE.h /Desktop/asn1c/asn_application.h /Desktop/asn1c/der_encoder.h /Desktop/asn1c/ber_tlv_length.h -o ecoder_test
The issue is on compile, it is not recognizing der_type_encoder_f which is being declared in der_encoder.h, (I've only included the declaration of that file)
typedef asn_enc_rval_t (der_type_encoder_f) (
struct asn_TYPE_descriptor_s *type_descriptor,
void *struct_ptr, /* Structure to be encoded */
int tag_mode, /* {-1,0,1}: IMPLICIT, no, EXPLICIT */
ber_tlv_tag_t tag,
asn_app_consume_bytes_f *consume_bytes_cb, /* Callback */
void *app_key /* Arbitrary callback argument */
);
My questions are,
1) does the order of files in the Makefile make a difference, such that on compile, it wont recognize the type? and if so, why?
2) Can the order things are included in the main program cause the same problem?

What are the benefits of unnamed structs / unions in C?

I found one code implemented as the similar demo shown below ..
struct st
{
int a;
struct
{
int b;
};
};
6.58 Unnamed struct/union fields within structs/unions
As permitted by ISO C11.
But What are benefits of it ?
Because anyway I can access the data members in a same manner like
int main()
{
struct st s;
s.a=11;
s.b=22;
return 0;
}
compiled on gcc 4.5.2 with ,
gcc -Wall demo.c -o demo
and no errors ,
It does not have to be an anonymous struct inside a struct, which I do not find very useful: this will typically only change the layout slightly by introducing more padding, with no other visible effects (compared to inlining the members of the child struct into the parent struct).
I think that the advantage of anonymous struct/unions is elsewhere:
they can be used to place an anonymous struct inside an union or an anonymous union inside a struct.
Example:
union u
{
int i;
struct { char b1; char b2; char b3; char b4; };
};
The benefit is pretty obvious, isn't it? It saves the programmer from coming up with a name! Since naming things is hard, it's nice that it's possible to avoid doing so if there is no real need.
It's also a pretty clear signal that this struct is local and never used anywhere else but in the context of being a field in the parent struct, which is really, really nice information since it reduces the possibility of needless coupling.
Think of it as static; it restricts the visibility of the inner struct to the outer one, in a manner similar to (but not, of course, equivalent with) how static restricts the visibility of global symbols to the compilation unit in which they appear.
I just ran into a huge benefit of anonymous union. However be warned this is not a story for the faint hearted nor is it a recommended practice.
Note: See also Anonymous union within struct not in c99?
In an older C program of hundreds of source code files there is a global variable, a struct, which contained a struct as a member. So the type definition for the global variable looked some thing like:
typedef struct {
LONG lAmount;
STRUCTONE largeStruct; // memory area actually used for several different struct objects
ULONG ulFlags;
} STRUCTCOMMON;
The struct, STRUCTONE, was one of several large structs however the others were all smaller than STRUCTONE at the time this code was written. So this memory area, largeStruct was being used as a union but without the proper source statements indicating so. Instead various struct variables were copied into this area using memcpy(). To make matters worse sometimes this was through the actual name of the global variable and sometimes through a pointer to the global variable.
As typically happens as time progresses recent changes resulted in one of the other structs becoming the largest. And I was faced with having to go through a hundred files looking for where this was being used along with all the various aliases and everything else.
And then I remembered anonymous unions. So I modified the typedef to be the following:
typedef struct {
LONG lAmount;
union {
// anonymous union to allow for allocation of largest space needed
STRUCTONE largeStruct; // memory area actually used for several different struct objects
STRUCTTHREE largerStruct; // memory area for even larger struct
};
ULONG ulFlags;
} STRUCTCOMMON;
And then recompiled every thing.
So now all those days of source code review and regression testing I was unhappily looking forward to are no longer necessary.
And I can now begin the process of slowly modifying source using this global to bring this source up to more modern standards on my own time table.
Addendum - Anonymous struct within anonymous union
Working in this same source code body I ran into an application of this technique with a binary record that could contain date from one of several different structs which were supposed to be the same length. The problem I found was due to a programmer error, one struct was a different size than the others.
As part of correcting this problem, I wanted a solution that would allow the compiler to figure out the correct sizes for the data structures.
Since these structs contained some differences in a couple of members of the structs with padding variables added to make them all the same size, I went with anonymous unions which worked fine except for one of the structs.
I found I could add an anonymous struct as part of the union so that as long as the various members of the union and the added anonymous struct had different names, it would compile fine with Visual Studio 2015.
Important Note: This solution requires #pragma pack(1) with Visual Studio 2015 to pack the structs and unions on byte boundaries. Without the use of the pragma the compiler may introduce unknown padding into the various structs and unions.
I created the following define in order to standardize the anonymous union and anonymous struct.
#define PROGRPT_UNION_STRUCT \
union { \
SHORT sOperand1; /* operand 1 (SHORT) */ \
LONG lOperand1; /* operand 1 (LONG) */ \
PROGRPT_ITEM Operand1; /* operand 1 */ \
struct { \
UCHAR uchReserved3; /* */ \
USHORT usLoopEnd; /* offset for loop end */ \
UCHAR uchReserved4; /* */ \
}; \
};
Then used it as in this sample of three of the several structs that are used to access the binary data in the data record read from a file.
/* loop record */
typedef struct {
UCHAR uchOperation; /* operation code (LOOP) */
UCHAR uchRow; /* position (row) */
UCHAR uchLoopBrace; /* loop brace (begin/end) */
UCHAR uchReserved1; /* */
TCHAR auchReserved2[ 2 ]; /* */
UCHAR uchCondition; /* condition code */
PROGRPT_ITEM LoopItem; /* loop record */
PROGRPT_UNION_STRUCT
PROGRPT_ITEM Reserved5; /* */
} PROGRPT_LOOPREC;
/* print record */
typedef struct {
UCHAR uchOperation; /* operation code (PRINT) */
UCHAR uchRow; /* position (row) */
UCHAR uchColumn; /* position (column) */
UCHAR uchMaxColumn; /* max no of column */
TCHAR auchFormat[ 2 ]; /* print format/style */
UCHAR uchCondition; /* condition code */
PROGRPT_ITEM PrintItem; /* print item */
PROGRPT_UNION_STRUCT
PROGRPT_ITEM Operand2; /* ope2 for condition */
} PROGRPT_PRINTREC;
/* mathematics record ( accumulator.total = LONG (+,-,*,/) opr2) */
typedef struct {
UCHAR uchOperation; /* operation code (MATH) */
UCHAR uchRow; /* position (row) */
UCHAR uchColumn; /* position (column) */
UCHAR uchMaxColumn; /* max no of column */
TCHAR auchFormat[ 2 ]; /* format style */
UCHAR uchCondition; /* condition code */
PROGRPT_ITEM Accumulator; /* accumulator */
PROGRPT_UNION_STRUCT
PROGRPT_ITEM Operand2; /* operand 2 */
} PROGRPT_MATHTTL;
which were originally
typedef struct {
UCHAR uchOperation; /* operation code (LOOP) */
UCHAR uchRow; /* position (row) */
UCHAR uchLoopBrace; /* loop brace (begin/end) */
UCHAR uchReserved1; /* */
TCHAR auchReserved2[ 2 ]; /* */
UCHAR uchCondition; /* condition code */
PROGRPT_ITEM LoopItem; /* loop record */
UCHAR uchReserved3; /* */
USHORT usLoopEnd; /* offset for loop end */
UCHAR uchReserved4; /* */
PROGRPT_ITEM Reserved5; /* */
} PROGRPT_LOOPREC;
/* print record */
typedef struct {
UCHAR uchOperation; /* operation code (PRINT) */
UCHAR uchRow; /* position (row) */
UCHAR uchColumn; /* position (column) */
UCHAR uchMaxColumn; /* max no of column */
TCHAR auchFormat[ 2 ]; /* print format/style */
UCHAR uchCondition; /* condition code */
PROGRPT_ITEM PrintItem; /* print item */
PROGRPT_ITEM Operand1; /* ope1 for condition */
PROGRPT_ITEM Operand2; /* ope2 for condition */
} PROGRPT_PRINTREC;
/* mathematics record ( accumulator.total = LONG (+,-,*,/) opr2) */
typedef struct {
UCHAR uchOperation; /* operation code (MATH) */
UCHAR uchRow; /* position (row) */
UCHAR uchColumn; /* position (column) */
UCHAR uchMaxColumn; /* max no of column */
TCHAR auchFormat[ 2 ]; /* format style */
UCHAR uchCondition; /* condition code */
PROGRPT_ITEM Accumulator; /* accumulator */
LONG lOperand1; /* operand 1 (LONG) */
PROGRPT_ITEM Operand2; /* operand 2 */
} PROGRPT_MATHTTL;
Using a union of all the various record types that looks like:
typedef union {
PROGRPT_LOOPREC Loop; /* loop record */
PROGRPT_PRINTREC Print; /* print record */
PROGRPT_MATHOPE MathOpe; /* math (with operand) */
PROGRPT_MATHTTL MathTtl; /* math (with total) */
PROGRPT_MATHCO MathCo; /* math (with count) */
} PROGRPT_RECORD;
These record formats are used in the code similar to the following:
for ( usLoopIndex = 0; usLoopIndex < usMaxNoOfRec; ) {
ULONG ulActualRead = 0; /* actual length of read record function */
PROGRPT_RECORD auchRecord;
/* --- retrieve a formatted record --- */
ProgRpt_ReadFile( ulReadOffset, &auchRecord, PROGRPT_MAX_REC_LEN, &ulActualRead );
if ( ulActualRead != PROGRPT_MAX_REC_LEN ) {
return ( LDT_ERR_ADR );
}
/* --- analyze operation code of format record, and
store it to current row item buffer --- */
switch ( auchRecord.Loop.uchOperation ) {
case PROGRPT_OP_PRINT: /* print operation */
sRetCode = ProgRpt_FinPRINT( &ReportInfo, &auchRecord.Print, uchMinorClass, NULL );
break;
case PROGRPT_OP_MATH: /* mathematics operation */
sRetCode = ProgRpt_FinMATH(&auchRecord.MathOpe, NULL );
break;
case PROGRPT_OP_LOOP: /* loop (begin) operation */
ProgRpt_PrintLoopBegin( &ReportInfo, &auchRecord.Loop );
switch ( auchRecord.Loop.LoopItem.uchMajor ) {
case PROGRPT_INDKEY_TERMNO:
sRetCode = ProgRpt_IndLOOP( &ReportInfo, &auchRecord.Loop, uchMinorClass, usTerminalNo, ulReadOffset );
usLoopIndex += auchRecord.Loop.usLoopEnd;
ulReadOffset += ( PROGRPT_MAX_REC_LEN * auchRecord.Loop.usLoopEnd );
break;
default:
return ( LDT_ERR_ADR );
}
break;
default:
return ( LDT_ERR_ADR );
}
// .......
I've used anonymous structs in developing contiguous address structures that I'll be accessing via pointers. More specifically, I'll use anonymous structs within the parent struct to enable bit-fielding certain portions of the memory that is divided into smaller portions of labeled data.
Be careful of how your compiler packs the bit-fielded information, the first member of the bitfielded struct can either be the LSB or MSB.
typedef struct
{
uint32_t a;
struct
{
uint32_t b : 16;
uint32_t c : 8;
uint32_t d : 7;
uint32_t e : 1;
};
}Parent;
#define ADDRESS ((Parent*)(uint16_t)0xF0F0)
ADDRESS->a = data_32_bits;
ADDRESS->b = data_16_bits;
ADDRESS->c = data_8_bits;
ADDRESS->d = data_7_bits;
ADDRESS->e = data_1_bit;

Resources