[ofw] [RFC] 2/5: IB ACM: windows abstractions

Sean Hefty sean.hefty at intel.com
Wed Sep 16 23:27:34 PDT 2009


The following abstractions are defined to support the IB ACM running on Windows.

An attempt was made to limit the number of dependencies on external libraries,
such as complib.  We add Windows support for the Linux 'search' binary
tree interfaces.  This is implemented on Windows using complib fleximap, but
gets linked in statically.

Signed-off-by: Sean Hefty <sean.hefty at intel.com>
---
/*
 * Copyright (c) 2009 Intel Corporation.  All rights reserved.
 *
 * This software is available to you under the OpenFabrics.org BSD license
 * below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AWV
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#if !defined(OSD_H)
#define OSD_H

#include <windows.h>
#include <process.h>
#include <winsock2.h>

#define __func__ __FUNCTION__
#define LIB_DESTRUCTOR
#define CDECL_FUNC __cdecl

typedef struct { volatile LONG val; } atomic_t;
#define atomic_inc(v) InterlockedIncrement(&(v)->val)
#define atomic_dec(v) InterlockedDecrement(&(v)->val)
#define atomic_get(v) ((v)->val)
#define atomic_set(v, s) ((v)->val = s)

#define event_t			HANDLE
#define event_init(e)	*(e) = CreateEvent(NULL, FALSE, FALSE, NULL)
#define event_signal(e)	SetEvent(*(e))
#define event_wait(e, t) WaitForSingleObject(*(e), t)	

#define lock_t			CRITICAL_SECTION
#define lock_init		InitializeCriticalSection
#define lock_acquire	EnterCriticalSection
#define lock_release	LeaveCriticalSection

static __inline int osd_init()
{
	WSADATA wsadata;
	return WSAStartup(MAKEWORD(2, 2), &wsadata);
}

static __inline void osd_close()
{
	WSACleanup();
}

#define stricmp _stricmp
#define strnicmp _strnicmp

#define socket_errno WSAGetLastError
#define SHUT_RDWR SD_BOTH

static __inline UINT64 time_stamp_us(void)
{
	LARGE_INTEGER cnt, freq;
	QueryPerformanceFrequency(&freq);
	QueryPerformanceCounter(&cnt);
	return (UINT64) cnt.QuadPart / freq.QuadPart * 1000000;
}

#define time_stamp_ms() (time_stamp_us() * 1000)

#define getpid() ((int) GetCurrentProcessId())
#define beginthread(func, arg)	(int) _beginthread(func, 0, arg)
#define container_of CONTAINING_RECORD

#endif /* OSD_H */


/*
 * Copyright (c) 2009 Intel Corp, Inc.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

#ifndef _SEARCH_H_
#define _SEARCH_H_

#include <complib/cl_fleximap.h>

//typedef enum
//{
//	preorder,
//	postorder,
//	endorder,
//	leaf
//
//}	VISIT;

void *tsearch(const void *key, void **rootp,
			  int (*compar)(const void *, const void *));
void *tfind(const void *key, void *const *rootp,
			int (*compar)(const void *, const void *));
/* tdelete returns key if found (not parent), otherwise NULL */
void *tdelete(const void *key, void **rootp,
			  int (*compar)(const void *, const void *));
//void twalk(const void *root,
//		   void (*action)(const void *, VISIT, int));
//void tdestroy(void *root, void (*free_node)(void *nodep));

#endif	/* _SEARCH_H_ */


/*
 * Copyright (c) 2009 Intel Corp., Inc.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

#include <stdlib.h>
#include <search.h>

static int (*compare)(const void *, const void *);

static intn_t fcompare(const void * const key1, const void * const key2)
{
	return (intn_t) compare((void *) key1, (void *) key2);
}

void *tsearch(const void *key, void **rootp,
			  int (*compar)(const void *, const void *))
{
	cl_fmap_item_t *item, *map_item;

	if (!*rootp) {
		*rootp = malloc(sizeof(cl_fmap_t));
		cl_fmap_init((cl_fmap_t *) *rootp, fcompare);
	}

	compare = compar;
	item = malloc(sizeof(cl_fmap_item_t));
	map_item = cl_fmap_insert((cl_fmap_t *) *rootp, key, item);
	if (map_item != item)
		free(item);

	return (void *) &map_item->p_key;
}

void *tfind(const void *key, void *const *rootp,
			int (*compar)(const void *, const void *))
{
	cl_fmap_item_t	*item;

	if (!*rootp)
		return NULL;

	compare = compar;
	item = cl_fmap_get((cl_fmap_t *) *rootp, key);
	if (item == cl_fmap_end((cl_fmap_t *) *rootp))
		return NULL;

	return (void *) &item->p_key;
}

/*
 * Returns NULL if item is not found, or the item itself.  This differs
 * from the tdelete call by not retuning the parent item, but works if
 * the user is only checking against NULL.
 */
void *tdelete(const void *key, void **rootp,
			  int (*compar)(const void *, const void *))
{
	cl_fmap_item_t	*item;
	void *map_key;

	if (!*rootp)
		return NULL;

	compare = compar;
	item = cl_fmap_remove((cl_fmap_t *) *rootp, key);
	if (item == cl_fmap_end((cl_fmap_t *) *rootp))
		return NULL;

	map_key = (void *) item->p_key;
	free(item);
	return map_key;
}

//void twalk(const void *root,
//		   void (*action)(const void *, VISIT, int))
//{
//}

//void tdestroy(void *root, void (*free_node)(void *nodep))
//{
//}





More information about the ofw mailing list