[xiph-cvs] cvs commit: vorbis/lib dllmain.c shmmap.h shmmap_c.h
Chris Wolf
cwolf at xiph.org
Fri Sep 7 01:44:59 PDT 2001
cwolf 01/09/07 01:44:59
Added: lib dllmain.c shmmap.h shmmap_c.h
Log:
Fix win32 access violation in vorbisenc
Revision Changes Path
1.1 vorbis/lib/dllmain.c
Index: dllmain.c
===================================================================
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
* by the XIPHOPHORUS Company http://www.xiph.org/ *
********************************************************************
function:
User DllMain to allow standalone vorbisenc.dll to access
backend mapping structures in defined in registery.c
and built into vorbis.dll, by copying them into shared
memory upon DLL loading.
created: 06-Sep-2001, Chris Wolf
Adopted from "dllskel" MSDN COM tutorial
last mod: $Id: dllmain.c,v 1.1 2001/09/07 08:44:59 cwolf Exp $
This module is used in both vorbis.dll and vorbisenc.dll. If vorbisenc.c
is built into a separate DLL, then define STANDALONE_VORBISENC_DLL so
that the memory mapping takes place. If this is not defined (as would
be the case when building one single DLL, the the pointers are accessed
normally.
When this module is compiled into vorbis.dll, with VORBIS_DLL defined,
it makes DllMain initialize the shared memory segment.
When this module is compiled into vorbisenc.dll, with VORBIS_DLL
not defined, it makes DllMain retrieve a pointer to the already
initialized shared memory segment.
If STANDALONE_VORBISENC_DLL is not defined, then DllMain is a no-op
for either DLL.
********************************************************************/
#ifdef _MSC_VER // This file is meaningless outside MSC
#include <windows.h>
#include <memory.h>
#include "shmmap.h"
#define DLLSHARED_STR "vorbis_dll" // Name for file-mapped segement
BOOL initializeProcess(HINSTANCE hDll);
BOOL cleanupProcess(HINSTANCE hDll);
SHARED_MAP* g_shared_map;
static void* g_pvShared;
static HANDLE hShm;
BOOL WINAPI DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID lpvReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
#ifdef STANDALONE_VORBISENC_DLL
return (initializeProcess(hDll));
#endif
break;
case DLL_PROCESS_DETACH:
#ifdef STANDALONE_VORBISENC_DLL
cleanupProcess (hDll);
#endif
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
default:
break;
}
return TRUE;
}
BOOL initializeProcess(HINSTANCE hDll)
{
BOOL isFirstMapping = TRUE;
int iSharedSize=1;
#ifdef VORBIS_DLL
SHARED_MAP *map = table_map2mem(&iSharedSize);
#endif
// Create a named file mapping object.
hShm = CreateFileMapping(
(HANDLE) 0xFFFFFFFF, // Use paging file
NULL, // No security attributes
PAGE_READWRITE, // Read/Write access
0, // Mem Size: high 32 bits
iSharedSize, // Mem Size: low 32 bits
DLLSHARED_STR); // Name of map object
if (NULL == hShm)
return FALSE;
// Determine if this is the first create of the file mapping.
isFirstMapping = (ERROR_ALREADY_EXISTS != GetLastError());
// Now get a pointer to the file-mapped shared memory.
g_pvShared = MapViewOfFile(
hShm, // File Map obj to view
FILE_MAP_WRITE, // Read/Write access
0, // high: map from beginning
0, // low:
0); // default: map entire file
if (NULL != g_pvShared)
{
#ifdef VORBIS_DLL
if (isFirstMapping)
{
// If this is the first process attaching vorbis.dll,
// initialize the shared memory.
(void)memcpy(g_pvShared, (void *)map, iSharedSize);
}
#else
// If this is a process attaching to vorbisenc.dll
// get the pointer tables from shared memory
g_shared_map = (SHARED_MAP *)g_pvShared;
#endif
}
return TRUE;
}
BOOL cleanupProcess(HINSTANCE hDll)
{
// Unmap any shared memory from the process's address space.
UnmapViewOfFile(g_pvShared);
// Close the process's handle to the file-mapping object.
CloseHandle(hShm);
return TRUE;
}
#endif /* _MSC_VER */
1.1 vorbis/lib/shmmap.h
Index: shmmap.h
===================================================================
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
* by the XIPHOPHORUS Company http://www.xiph.org/ *
********************************************************************
function: Defines the structure of the shared memory segment.
Any new globals added to vorbis.dll which need to accessed
by other DLLs (vorbisenc.dll) should be added here.
created: 06-Sep-2001, Chris Wolf
last mod: $Id: shmmap.h,v 1.1 2001/09/07 08:44:59 cwolf Exp $
********************************************************************/
#ifndef _shmmap_h_
# define _shmmap_h_
#include "codec_internal.h"
#define NUMELEMENTS(x) (sizeof(x)/sizeof(x[0]))
#pragma pack(push, shared_map, 4) // use a known structure alignment
typedef struct shared_map
{
vorbis_func_time **p_time_P;
vorbis_func_floor **p_floor_P;
vorbis_func_residue **p_residue_P;
vorbis_func_mapping **p_mapping_P;
} SHARED_MAP;
#pragma pack(pop, shared_map)
extern SHARED_MAP* g_shared_map;
extern SHARED_MAP* table_map2mem(int *len);
#endif /* _shmmap_h_ */
1.1 vorbis/lib/shmmap_c.h
Index: shmmap_c.h
===================================================================
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
* by the XIPHOPHORUS Company http://www.xiph.org/ *
********************************************************************
function: Creates an image of strutures and mappings in registry.c
to be copied into shared memory segment via DllMain().
See: dllmain.c
created: 06-Sep-2001, Chris Wolf
last mod: $Id: shmmap_c.h,v 1.1 2001/09/07 08:44:59 cwolf Exp $
This module gets conditionally appended to the end of registry.c
********************************************************************/
#include <malloc.h>
#include <memory.h>
#include "shmmap.h"
/**
* Create image of mappings defined in regsitry.c.
*
* PARAM len: pointer to callers length indicator, will indicate
* size of shared map.
* RETURN SHARED_MAP: pointer to allocated mapping image, NULL on failure.
*/
SHARED_MAP* table_map2mem(int *len)
{
SHARED_MAP* map;
int p;
int size;
if ((map = calloc(1, sizeof(SHARED_MAP))) == (SHARED_MAP*)0)
return (SHARED_MAP*)0;
size = sizeof(SHARED_MAP);
if ((map->p_time_P = calloc(NUMELEMENTS(_time_P),
sizeof(vorbis_func_time*))) == (vorbis_func_time**)0)
return (SHARED_MAP*)0;
size += sizeof(_time_P);
for (p=0; p<NUMELEMENTS(_time_P); p++)
{
if ((map->p_time_P[p] = calloc(NUMELEMENTS(_time_P),
sizeof(vorbis_func_time))) == (vorbis_func_time*)0)
return (SHARED_MAP*)0;
(void)memcpy((void *)map->p_time_P[p],
(const void *)_time_P[p],
sizeof(vorbis_func_time));
size += sizeof(vorbis_func_time);
}
if ((map->p_floor_P = calloc(NUMELEMENTS(_floor_P),
sizeof(vorbis_func_floor*))) == (vorbis_func_floor**)0)
return (SHARED_MAP*)0;
size += sizeof(_floor_P);
for (p=0; p<NUMELEMENTS(_floor_P); p++)
{
if ((map->p_floor_P[p] = calloc(1,
sizeof(vorbis_func_floor))) == (vorbis_func_floor*)0)
return (SHARED_MAP*)0;
(void)memcpy((void *)map->p_floor_P[p],
(const void *)_floor_P[p],
sizeof(vorbis_func_floor));
size += sizeof(vorbis_func_floor);
}
if ((map->p_residue_P = calloc(NUMELEMENTS(_residue_P),
sizeof(vorbis_func_residue*))) == (vorbis_func_residue**)0)
size += sizeof(_residue_P);
for (p=0; p<NUMELEMENTS(_residue_P); p++)
{
if ((map->p_residue_P[p] = calloc(1,
sizeof(vorbis_func_residue))) == (vorbis_func_residue*)0)
return (SHARED_MAP*)0;
(void)memcpy((void *)map->p_residue_P[p],
(const void *)_residue_P[p],
sizeof(vorbis_func_residue));
size += sizeof(vorbis_func_residue);
}
if ((map->p_mapping_P = calloc(NUMELEMENTS(_mapping_P),
sizeof(vorbis_func_mapping*))) == (vorbis_func_mapping**)0)
return (SHARED_MAP*)0;
size += sizeof(_mapping_P);
for (p=0; p<NUMELEMENTS(_mapping_P); p++)
{
if ((map->p_mapping_P[p] = calloc(1,
sizeof(vorbis_func_mapping))) == (vorbis_func_mapping*)0)
return (SHARED_MAP*)0;
(void)memcpy((void *)map->p_mapping_P[p],
(const void *)_mapping_P[p],
sizeof(vorbis_func_mapping));
size += sizeof(vorbis_func_mapping);
}
*len = size;
return (map);
}
--- >8 ----
List archives: http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body. No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.
More information about the commits
mailing list