Writing drivers in C++

Operator new and delete

The new and delete operators may be used within driver code to allocate and release memory. However the default system implementation of these operators (as contained within libC, the C++ standard library) cannot be used. This is because these implementations rely on C standard library calls such as malloc( ) and free( ), which are not accessible from within the kernel environment.

Instead, you must code versions of the new and delete operators as part of the driver code. Fortunately, in the C++ language, this can be done in a standard way: by replacing the global new and delete operators, by provided class-specific overloaded new and delete operators, or by using a placement new operator. The semantics of these operators are documented in the Section 3C++ manual pages. See ``Memory allocation'' for information about the memory allocation facilities supported for SVR5, SCO SVR5 2.X, and SCO OpenServer 5 device drivers.

Note that the SVR5 C++ implementation supports the ``array new/delete'' language feature so that separate replacements or overloadings must be done for the new and delete operators for arrays versus scalars.

The SVR5 C++ implementation also supports the ``nothrow new'' language feature. These are versions of the operators that are defined to return zero upon memory allocation failure rather than throw an exception (which is what the basic operators now do). Because driver code cannot use exceptions, code the driver using the ``nothrow'' forms. The basic forms will also work, but will be less clear and less portable.

Thus, in SVR5, these are the signatures that you may have to replace:

   operator new(size_t,std::nothrow_t&)          // nothrow forms
   operator new[](size_t,std::nothrow_t&)
   operator delete(void*,std::nothrow_t&)
   operator delete[](void*,std::nothrow_t&)

operator new(size_t) // basic forms operator new[](size_t) operator delete(void*) operator delete[](void*)

Do not use the set_new_handler C++ standard library function in device drivers because the implementation of the function relies upon static variables and threads support that is not supported in the kernel environment.

If the driver code uses new and delete for arrays, link in the vec_newdel.o runtime support routines as described in ``Runtime support routines''. These routines do not do any allocation or deallocation themselves, but rather take care of remembering how much storage is allocated for an array, and invoke the constructors/destructors for each element of an array. The compiler generates different kinds of constructor and destructor routines, some with hidden arguments, so the semantics of the vec_newdel.o routines can get quite complicated, so you do not want to try to replace them with routines you write yourself.

© 2005 The SCO Group, Inc. All rights reserved.
OpenServer 6 and UnixWare (SVR5) HDK - June 2005