473,782 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing a tag pointer class

Hello world,

I'm trying to implement a (hopefully portable!) tagged pointer class.
Basically, I have my own allocator which will ensure alignment at 8-
byte boundaries (where "byte" is "size of a char"), and allocates
objects of type Generic. My tagged pointer class will support either
Generic* (tag = 0b000) or small integers (tag=0b001) for now (in the
future maybe add unicode chars, and/or Cons* etc.).

A rough sketch of what I intend to do:

class Generic;

class Object {
//public for now, will private this and make the
//external functions friends later
public:
union types {
void* ptr;
int num;
};
types dat;

Object(void* x) {
// in case sizeof(void*) != sizeof(int)
if(sizeof(int) sizeof(void*)) {
dat.num = 0;
}
dat.ptr = x;
}
Object(int x) {
if(sizeof(void* ) sizeof(int)) {
dat.ptr = 0;
}
dat.num = x;
}

public:
static inline Object smallint(int x) {
x = (x << 3) + 0x1;
return Object(x);
}
static inline Object obj(Generic* x) {
if(x & 0x7) throw AlignmentError( );
char* tmp = ((char*)(void*) x) + 0x0;
return Object((void*) tmp);
}
};

static inline char tag(Object x) {
if(sizeof(void* ) sizeof(int)) {
return (char)(((long) x.dat.ptr) & 0x7);
} else {
return (char)(x.dat.nu m & 0x7);
}
}

static inline bool is_smallint(Obj ect x) {
return tag(x) == 0x1;
}
static inline bool is_obj(Object x) {
return tag(x) == 0x0;
}

static inline int as_smallint(Obj ect x) {
if(!is_smallint (x)) throw TypeError();
return x.dat.num >3;
}

static inline Generic* as_obj(Object x) {
if(!is_obj(x)) throw TypeError();
char* tmp = ((char*) x.dat.ptr) - 0x0;
return (Generic*)(void *)tmp;
}

I've tested a version of the above code in a 32-bit x86 GNU/Linux
system with gcc, but I wonder if it's portable, say to big-endian
machines. Also, if optimizations are turned on (-O), it seems to
generate code which approximately looks like what I would expect for
explicit tagged pointers.

I would prefer to use a class-based solution for cleanliness, but I'm
concerned about using unions of potentially differently-sized objects,
especially if the code ends up in a big-endian system with a different
size for pointers and int.

I could also try to dig out the class/type/etc. I saw once which is
supposed to be an integral type that is the same size as the smallest
integer that can fit a void*.

Any pointers and suggestions, as well as analyses on how well this
might perform on various systems/compilers are welcome.

Sincerely,
AmkG
Oct 29 '08 #1
5 3130
alan wrote:
I'm trying to implement a (hopefully portable!) tagged pointer class.
[..]
static inline Object obj(Generic* x) {
if(x & 0x7) throw AlignmentError( );
How does this compile? AFAIUI the binary operator & is not defined for
pointer types...
char* tmp = ((char*)(void*) x) + 0x0;
What's the point of adding 0?
return Object((void*) tmp);
}
};
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 29 '08 #2
Victor Bazarov wrote:
alan wrote:
>I'm trying to implement a (hopefully portable!) tagged pointer class.
[..]
static inline Object obj(Generic* x) {
if(x & 0x7) throw AlignmentError( );
Yeah, he might want to cast x to an int
How does this compile? AFAIUI the binary operator & is not defined for
pointer types...
> char* tmp = ((char*)(void*) x) + 0x0;

What's the point of adding 0?
To add: what's point in doing (char*)(void*) ?
Oct 29 '08 #3
On Oct 29, 11:01*pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
alan wrote:
I'm trying to implement a (hopefully portable!) tagged pointer class.
[..]
* * static inline Object obj(Generic* x) {
* * * * if(x & 0x7) throw AlignmentError( );

How does this compile? *AFAIUI the binary operator & is not defined for
pointer types...
Sorry, must have forgot to insert the cast at this point.
>
* * * * char* tmp = ((char*)(void*) x) + 0x0;

What's the point of adding 0?
It's a pattern which I intend to copy for, say, types other than
Generic*. Say Cons*. Or Sym*. The 0x0 is the tag. I can probably
replace the 0x0 with something like:

#define GENERIC_TAG 0x0
#define CONS_TAG 0x2

.....

char* tmp = ((char*)(void*) x) + GENERIC_TAG;

.....

char* tmp = ((char*)(void*) x) + CONS_TAG;

....later on.
>
* * * * return Object((void*) tmp);
* * }
};
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 29 '08 #4
On Oct 29, 11:01*pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
alan wrote:
I'm trying to implement a (hopefully portable!) tagged pointer class.
[..]
* * static inline Object obj(Generic* x) {
* * * * if(x & 0x7) throw AlignmentError( );

How does this compile? *AFAIUI the binary operator & is not defined for
pointer types...
Yes, I probably want to add a cast there.
>
* * * * char* tmp = ((char*)(void*) x) + 0x0;

What's the point of adding 0?
It's a pattern I want to copy for other types. Say Cons*. Or Sym*.

I could do:

#define GENERIC_TAG 0x0
#define CONS_TAG 0x2

....

char* tmp = ((char*)(void*) x) + GENERIC_TAG;

....

char* tmp = ((char*)(void*) x) + CONS_TAG;
>
* * * * return Object((void*) tmp);
* * }
};
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 29 '08 #5
On Oct 30, 12:56*am, anon <a...@no.invali dwrote:
Victor Bazarov wrote:
alan wrote:
I'm trying to implement a (hopefully portable!) tagged pointer class.
[..]
* * static inline Object obj(Generic* x) {
* * * * if(x & 0x7) throw AlignmentError( );

Yeah, he might want to cast x to an int
How does this compile? *AFAIUI the binary operator & is not defined for
pointer types...
* * * * char* tmp = ((char*)(void*) x) + 0x0;
What's the point of adding 0?

To add: what's point in doing (char*)(void*) ?
Sorry, I wasn't aware of reinterpret_cas t at the time.
Oct 29 '08 #6

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

Similar topics

1
3601
by: kscho | last post by:
Hi~ i wanna implement like COM interface, but I have problem pass this pointer. How can i pass this pointer in C ? How can i pass this pointer in CPP (thus not occured error)? //_______ test1.cpp _______________________________________
15
426
by: Bernard | last post by:
Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions have been asked on clc before so it is probably OK. I am a newbie to C++. BS 3rd edition states: % The throw transfers control to a handler for exceptions .... %
4
1899
by: mohan | last post by:
Hi All, How to implement virtual concept ( dynamic polymorphism ) in c. I guess i should create a void pointer which is pointing to the function. Not clear about this Does anyone have some idea Mohan
27
2176
by: Pete | last post by:
I'm doing exercise 8-2 from "Accelerated C++" where we're supposed to implement library algorithms, trying to minimize the number of iterator operations. I have two questions so far. First, it took me a while to figure out how to get accumulate() to compile correctly: // begin and begin2 must both be input iterators.
4
2078
by: pat | last post by:
Hi, I have been asked for an exam question to implement the constructor, copy constructor and destructor for the following class that describes an n-by-n matrix containing n squared integer values. (Eg. a matrix of n rows and n colums): import <iostream.h> class myMatrix {
4
3334
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
32
2593
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return the size occupied by that datatype in memory in bytes. Thanks :) Abhishek Srivastava
13
5042
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one implement operator new/delete I can't see a way to indicate, on delete, how many objects must be destroyed (or how big the space is) - alternatively I can't figure out what are the alignment requirements so that the implementation, after calling my...
3
1636
by: Daniel Kraft | last post by:
Hi, I usually program in C++, but for a special project I do have to code in C (because it may be ported to embedded-like platforms where no C++ compiler exists). I do want to realize some kind of polymorphic behaviour, like this: I have some abstract base "class" Base, that is, a struct containing a vtable of function-pointers and possibly some common fields (but at the
0
9641
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.