C
Chris Thomasson
Can anybody notice any problems with the following example of an approach I
have been tinkering around with:
http://appcore.home.comcast.net/vzoom/malloc/example.cpp
Here is a code snippet of the main alignment functions I created; I think I
am going to end up actually using the logic...
__________________
// aligns a ptr from a raw ptr
static void* alignptrraw(
void const *ptr,
size_t offset,
ptrdiff_t sz) {
ptrdiff_t const base = (static_cast<unsigned char const*>(0) -
static_cast<unsigned char const*>(ptr)) + offset;
ptrdiff_t const align = (base + sz - 1) & -(sz);
if (align % sz) {
assert(! (align % sz));
return 0;
}
return static_cast<unsigned char*>(0) - align;
}
// get the size of an align
template<typename T>
static size_t alignsztype(size_t offset) {
return (sizeof(T) * 2) + 1 + offset;
}
// aligns a type ptr from a raw ptr
template<typename T>
static void* alignptrtype(
void const *ptr,
size_t const maxsz,
size_t offset,
ptrdiff_t sz = sizeof(T)) {
if (alignsztype<T>(offset) >= maxsz) {
assert(alignsztype<T>(offset) < maxsz);
return 0;
}
return alignptrraw(ptr, offset, sz);
}
______________
Any thoughts? Thanks for all your help!
have been tinkering around with:
http://appcore.home.comcast.net/vzoom/malloc/example.cpp
Here is a code snippet of the main alignment functions I created; I think I
am going to end up actually using the logic...
__________________
// aligns a ptr from a raw ptr
static void* alignptrraw(
void const *ptr,
size_t offset,
ptrdiff_t sz) {
ptrdiff_t const base = (static_cast<unsigned char const*>(0) -
static_cast<unsigned char const*>(ptr)) + offset;
ptrdiff_t const align = (base + sz - 1) & -(sz);
if (align % sz) {
assert(! (align % sz));
return 0;
}
return static_cast<unsigned char*>(0) - align;
}
// get the size of an align
template<typename T>
static size_t alignsztype(size_t offset) {
return (sizeof(T) * 2) + 1 + offset;
}
// aligns a type ptr from a raw ptr
template<typename T>
static void* alignptrtype(
void const *ptr,
size_t const maxsz,
size_t offset,
ptrdiff_t sz = sizeof(T)) {
if (alignsztype<T>(offset) >= maxsz) {
assert(alignsztype<T>(offset) < maxsz);
return 0;
}
return alignptrraw(ptr, offset, sz);
}
______________
Any thoughts? Thanks for all your help!