Thursday, April 1, 2010

Using STL collections in Photoshop...

Hello!%26lt;br /%26gt;%26lt;br /%26gt;Would you explain one moment. I need use a lot of STL collections in my Photoshop plug-in.%26lt;br /%26gt;%26lt;br /%26gt;First, I try to write something like%26lt;br /%26gt;---------------------------------------------------------------------%26lt;br /%26gt;#include %26lt;map%26gt;%26lt;br /%26gt;#include %26lt;string%26gt;%26lt;br /%26gt;%26lt;br /%26gt;...%26lt;br /%26gt;%26lt;br /%26gt; map%26lt;int,string%26gt; foo; // BANG!%26lt;br /%26gt;---------------------------------------------------------------------%26lt;br /%26gt;%26lt;br /%26gt;Next, I try to use Photoshop Basic Suite:%26lt;br /%26gt;%26lt;br /%26gt;---------------------------------------------------------------------%26lt;br /%26gt;#include %26lt;algorithm%26gt;%26lt;br /%26gt;%26lt;br /%26gt;namespace std {}%26lt;br /%26gt;using namespace std;%26lt;br /%26gt;%26lt;br /%26gt;#include %26lt;PIFormat.h%26gt;%26lt;br /%26gt;%26lt;br /%26gt;extern SPBasicSuite *g_sSPBasic;%26lt;br /%26gt;%26lt;br /%26gt;inline void * operator new ( const size_t size )%26lt;br /%26gt;{%26lt;br /%26gt; void *result = 0;%26lt;br /%26gt;%26lt;br /%26gt; if( 0 != g_sSPBasic )%26lt;br /%26gt; {%26lt;br /%26gt; void %26amp; nbsp; *temp;%26lt;br /%26gt;%26lt;br /%26gt; if( g_sSPBasic-%26gt;AllocateBlock( size, %26amp;result ) )%26lt;br /%26gt; {%26lt;br /%26gt; %26amp;nbsp ; result = 0;%26lt;br /%26gt; }%26lt;br /%26gt; else%26lt;br /%26gt; {%26lt;br /%26gt;#ifdef DEBUG%26lt;br /%26gt; %26amp;nbsp ; char *tmp = static_cast%26lt;char*%26gt;( result );%26lt;br /%26gt; %26amp;nbsp ; fill( tmp, tmp + size, 0xEB );%26lt;br /%26gt;#endif%26lt;br /%26gt; }%26lt;br /%26gt; }%26lt;br /%26gt;%26lt;br /%26gt; return result;%26lt;br /%26gt;}%26lt;br /%26gt;%26lt;br /%26gt;inline void operator delete ( void * ptr )%26lt;br /%26gt;{%26lt;br /%26gt; if( 0 != g_sSPBasic )%26lt;br /%26gt; {%26lt;br /%26gt; g_sSPBasic-%26gt;FreeB lock( ptr );%26lt;br /%26gt; }%26lt;br /%26gt;}%26lt;br /%26gt;%26lt;br /%26gt;// the same for operator new[] and operator delete[]%26lt;br /%26gt;---------------------------------------------------------------------%26lt;br /%26gt;%26lt;br /%26gt;Now simple STL collections can be created.%26lt;br /%26gt;%26lt;br /%26gt;Is there more laconic way to create STL collections?%26lt;br /%26gt;%26lt;br /%26gt;Moreover. This code is very dirty. For example, I even don't sure that Simple Suite address is constant between calls of plug-in entry point. So I think this is an awful dangerous code (just imagine the effect of two copies of of Adobe Photoshop using plug-in using this operators simultaneously).%26lt;br /%26gt;%26lt;br /%26gt;Do you have some ideas how to make it clean and working?Using STL collections in Photoshop...
Are you using PINew.h(cpp)? I would not use them and not override operator new and delete. You are probably having a mismatch of when the new and delete operators are getting executed.
Using STL collections in Photoshop...
1. don't create global objects!

2. standard operator new() can't return 0. throw std::bad_alloc instead



2 Pavel: welcome to developer[dog]akvis.com

2 Tom Ruark



Thanks for your support.



%26gt; Are you using PINew.h(cpp)?



What is PINew.h(cpp)? Yeah, I can guess that it's what I need.



I use Adobe Photoshop CS2 SDK. There are no files PINew*.*



To be more precise where can I find these files?

2 all



On dynamic C++ memory management in Adobe Photoshop plug-in



As there are some people interested in the question I'll try to explain a technique I use at present.



I have two memory allocators:

a) based on Adobe's Basic Suite for small blocks (under 2Kbytes)

b) based on Adobe's Handle Suite for large blocks (above 2Kbytes)



operator new and operator new[] allocate 4 additional bytes for every allocation, store in the first 4 bytes handle to memory allocator used for this new or new[] call, and return pointer after this handle.



operator delete and operator delete[] takes memory allocator handle from the pre-pointer 4 bytes, and dellocate memory using correct memory allocator.



This strategy works well.



Does anybody know a better dynamic memory management technique to use with Adobe SDK for Adobe Photoshop plug-ins?

If Photoshop Handle/Buffer suite unavailable, use malloc() instead, and store INVALID_HANDLE_VALUE in pre-pointer 4 bytes.



operator delete must compare stored handle with INVALID_HANDLE_VALUE and call Photoshop callbacks or free()

%26gt; If Photoshop Handle/Buffer suite unavailable, use malloc() instead



But malloc() always returns NULL in my tests. So I throw std::bad_alloc if Adobe's suites is unavailable.

class Foo

{

public:

Foo() : ptr( new int[100] ) {}

~Foo() { delete [] ptr; }

private:

int * ptr;

};



Foo foo;



void PLUGIN_ENTRY_POINT(...



During DLL run-time startup, Foo::Foo called, and you don't have pointers to PS funcs at this point.

Result: Unhandled exception.



Many third-party libs have hidden global objects (managers, caches etc).



if you don't have suites, call malloc(). If malloc() return NULL, throw std::bad_alloc. And don't forgot to implement no-throw operator new.

2 Ivan Kharin



%26gt; Result: Unhandled exception.



You got the point!



The thing is I prefer to get an error diagnostic exactly at the time the problem occurs. As you know it grants us developers (and possibly our QA teams) more chances to efficiently work around the issue.



The another not least important reason is that unhandled exception at program startup is much-much-better then one after two and half hours of hard work.



%26gt; And don't forgot to implement no-throw operator new.



When I need it I will certanly do it.



Great thanks for your feedback.
  • mask making
  • No comments:

    Post a Comment