Skip to content

Instantly share code, notes, and snippets.

View jsbattig's full-sized avatar

Jose Sebastian Battig jsbattig

View GitHub Profile
@jsbattig
jsbattig / uWinVMMemoryStream.pas
Last active December 20, 2015 00:28
This class allows to create a TStream descendant in Delphi that allocates memory directly using VirtualAlloc allowing the developer to control the MemoryAttributes he/she wishes to use. I wrote this class in order to use with a JIT compiler that generated code using a TStream descendant object that was being blocked by DEP. With this you can cre…
unit uWinVMMemoryStream;
interface
uses
Classes, Windows;
type
TWinVMMemoryStream = class(TMemoryStream)
private
unit TestuWinVMMemoryStream;
{
Delphi DUnit Test Case
----------------------
This unit contains a skeleton test case class generated by the Test Case Wizard.
Modify the generated code to correctly setup and call the methods from the unit
being tested.
}
@jsbattig
jsbattig / svcbus_thread_pool.h
Created March 5, 2014 23:38
Thread Pooling class headers
/* Copyright 2013 Convey Compliance Systems, Inc.
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#ifndef SVCBUS_THREADPOOL_H
#define SVCBUS_THREADPOOL_H
#include <Windows.h>
@jsbattig
jsbattig / SvcBusThreadPoolTimer_init().c
Last active August 29, 2015 13:57
SvcBusThreadPoolTimer constructor method (with bug)
int SvcBusThreadPoolTimer_init( SvcBusThreadPoolTimer* _this, SvcBusThreadPool* pool, SvcBusThreadPool_callback callback, void* context, unsigned int millis )
{
FILETIME FileDueTime;
ULARGE_INTEGER ulDueTime;
_this->timer = CreateThreadpoolTimer( &SvcBusThreadPoolTimer_TimerCallback, _this, &pool->pcbe );
if( _this->timer == NULL )
{
return SVCBUS_THREADPOOL_ERROR;
}
@jsbattig
jsbattig / testThreadPoolTimer.c
Created March 5, 2014 23:57
ThreadPoolTimer test
TEST_F(SvcBusThreadPoolTest, SvcBusConsumer_testThreadPoolTimer) {
SvcBusThreadPoolTimer timer[1];
int int_value = 0;
ASSERT_TRUE(SvcBusThreadPoolTimer_init( timer, thread_pool, &TestCallback, &int_value, 100 ) == SVCBUS_THREADPOOL_OK);
EXPECT_EQ(0, SvcBusThreadPoolTimer_getThreadId( timer));
SvcBus_crossSleep(500);
EXPECT_TRUE( int_value > 0 );
EXPECT_NE(0, SvcBusThreadPoolTimer_getThreadId( timer));
SvcBusThreadPoolTimer_destroy( timer );
@jsbattig
jsbattig / SvcBusThreadPoolTimer_init() fixed.c
Created March 6, 2014 00:04
SvcBusThreadPoolTimer_init() fixed
int SvcBusThreadPoolTimer_init( SvcBusThreadPoolTimer* _this, SvcBusThreadPool* pool, SvcBusThreadPool_callback callback, void* context, unsigned int millis )
{
FILETIME FileDueTime;
ULARGE_INTEGER ulDueTime;
_this->timer = CreateThreadpoolTimer( &SvcBusThreadPoolTimer_TimerCallback, _this, &pool->pcbe );
if( _this->timer == NULL )
{
return SVCBUS_THREADPOOL_ERROR;
}
@jsbattig
jsbattig / uWin64ExceptionStack.pas
Created March 16, 2015 04:07
Delphi exception stack backup and restore class
unit uWin64ExceptionStack;
interface
{$IFDEF WIN64}
const
MAX_NESTED_EXCEPTIONS = 16;
type
@jsbattig
jsbattig / DelphiSwitchToFiber.pas
Last active July 28, 2023 18:55
Unit to properly SwitchToFiber() in Delphi without breaking exception handling
{
The idea of DelphiSwitchToFiber() function is to backup on a local variable in stack the
state of the Exception stack right before calling SwitchToFiber() and then restoring its state
right atfer returns from call to SwitchToFiber().
If SwitchToFiber() is used directly from within an Except or Finally block, and if there's an exception
raised after switching to another fiber, upon coming back the results will be unpredictable because
the exception stack will be completely unwinded and all raise exceptions destroyed.
In order to prevent this issue we must backup the Exception stack before the call to SwitchToFiber()
and restore it right after the call.
@jsbattig
jsbattig / BreakSwitchToFiber.pas
Created March 16, 2015 04:16
Sample test that will break fiber utilization in Delphi
procedure TestTJobQueue.JobMethodRaiseAndSwitchToFiber(const AJob: IJob; const AParams: array of Variant);
begin
try
Check(True);
raise ETestJobQueue.Create('Hello');
except
on E : Exception do
begin
try
raise ETestJobQueue.Create('Hello Nested');
@jsbattig
jsbattig / cpp_objects_constructor_and_assignment_operator_calling.cpp
Created October 13, 2015 21:28
Example in C++ of constructor, copy constructor and assignment operator calling
#include <stdio.h>
#include <string>
using namespace std;
class myString {
static int globalCounter;
string str;
public:
int counter;