Skip to content

Instantly share code, notes, and snippets.

View wholivesinapineappleunderthesea's full-sized avatar

ar wholivesinapineappleunderthesea

  • canada
  • 22:24 (UTC -04:00)
View GitHub Profile
// https://web.archive.org/web/20120401132446/http://bloglitb.blogspot.com/2011/12/access-to-private-members-safer.html
// This technique was invented by litb in 2010, long before
// templates were supercharged in C++17. Inspired by this method, I have
// reimplemented it using more modern C++ features.
//
// https://www.youtube.com/watch?v=SmlLdd1Q2V8
/*
14.7.2p8 The usual access checking rules do not apply to names used to specify
explicit instantiations. [Note: In particular, the template arguments and names
used in the function declarator (including parameter types, return types and
@wholivesinapineappleunderthesea
wholivesinapineappleunderthesea / COMPtr.h
Created June 10, 2023 00:27
COMPtr implementnationn
#pragma once
#include <Unknwn.h>
#include <exception>
template <typename T> struct COMPtrOut
{
inline COMPtrOut() = delete;
inline COMPtrOut(const COMPtrOut&) = delete;
@wholivesinapineappleunderthesea
wholivesinapineappleunderthesea / a.c
Created March 17, 2021 18:42
Allocate in a 32 bit area (Windows C/++)
static void* allocate_in_32bits(uint32_t start, uint32_t sz) {
if (!sz) return NULL;
MEMORY_BASIC_INFORMATION mbi = { 0 };
char* min = (char*)start;
char* max = (char*)UINT32_MAX - sz;
while (min < max) {
if (!VirtualQuery(min, &mbi, sizeof mbi)) return NULL;
if (mbi.State == MEM_FREE && mbi.RegionSize > sz && mbi.BaseAddress) {
void* alloc = VirtualAlloc(mbi.BaseAddress, sz, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if ((uint64_t)alloc > UINT32_MAX) { VirtualFree(alloc, 0, MEM_RELEASE); return NULL; }