dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/csupport .cvsignore,NONE,1.1 Complex


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/csupport .cvsignore,NONE,1.1 Complex.cs,NONE,1.1 Crt0.cs,NONE,1.1 FileTable.cs,NONE,1.1 LongDouble.cs,NONE,1.1 LongJmp.cs,NONE,1.1 Makefile.am,NONE,1.1 Markers.cs,NONE,1.1 README,NONE,1.1 csupport.build,NONE,1.1
Date: Sat, 07 Dec 2002 22:45:05 -0500

Update of /cvsroot/dotgnu-pnet/pnetlib/csupport
In directory subversions:/tmp/cvs-serv21784/csupport

Added Files:
        .cvsignore Complex.cs Crt0.cs FileTable.cs LongDouble.cs 
        LongJmp.cs Makefile.am Markers.cs README csupport.build 
Log Message:


Move the "csupport" directory from "pnet" to "pnetlib".


--- NEW FILE ---
Makefile
Makefile.in
.deps
*.dll

--- NEW FILE ---
/*
 * Complex.cs - Support classes for complex number arithmetic.
 *
 * This file is part of the Portable.NET "C language support" library.
 * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace OpenSystem.C
{

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Explicit, Size=8, Pack=4)]
[CName("float _Complex")]
public struct FloatComplex
{
        // Internal state.
        [FieldOffset(0)] private float real;
        [FieldOffset(4)] private float imag;

        // Constructors.
        public FloatComplex(float real, float imag)
                        {
                                this.real = real;
                                this.imag = imag;
                        }

        // Addition operator.
        public static FloatComplex operator+(FloatComplex a, FloatComplex b)
                        {
                                return new FloatComplex(a.real + b.real, a.imag 
+ b.imag);
                        }

        // Subtraction operator.
        public static FloatComplex operator-(FloatComplex a, FloatComplex b)
                        {
                                return new FloatComplex(a.real - b.real, a.imag 
- b.imag);
                        }

        // Multiplication operator.
        public static FloatComplex operator*(FloatComplex a, FloatComplex b)
                        {
                                return new FloatComplex
                                        (a.real * b.real - a.imag * b.imag,
                                         a.real * b.imag + a.imag * b.real);
                        }

        // Division operator.
        public static FloatComplex operator/(FloatComplex a, FloatComplex b)
                        {
                                float divisor = b.real * b.real + b.imag * 
b.imag;
                                return new FloatComplex
                                        ((a.real * b.real + a.imag * b.imag) / 
divisor,
                                         (a.imag * b.real - a.real * b.imag) / 
divisor);
                        }

        // Negation operator.
        public static FloatComplex operator-(FloatComplex a)
                        {
                                return new FloatComplex(-(a.real), -(a.imag));
                        }

        // Conjugate operator.
        public static FloatComplex operator~(FloatComplex a)
                        {
                                return new FloatComplex(a.real, -(a.imag));
                        }

        // Get the real and imaginary components.
        public float Real { get { return real; } }
        public float Imag { get { return imag; } }

        // Conversions.
        public static implicit operator FloatComplex(float value)
                        {
                                return new FloatComplex(value, 0.0f);
                        }
        public static implicit operator FloatComplex(FloatImaginary value)
                        {
                                return new FloatComplex(0.0f, value.Imag);
                        }

} // struct FloatComplex

[StructLayout(LayoutKind.Explicit, Size=4, Pack=4)]
[CName("float _Imaginary")]
public struct FloatImaginary
{
        // Internal state.
        [FieldOffset(0)] private float imag;

        // Constructors.
        public FloatImaginary(float imag)
                        {
                                this.imag = imag;
                        }

        // Get the imaginary component.
        public float Imag { get { return imag; } }

} // struct FloatImaginary

[StructLayout(LayoutKind.Explicit, Size=16, Pack=8)]
[CName("double _Complex")]
public struct DoubleComplex
{
        // Internal state.
        [FieldOffset(0)] private double real;
        [FieldOffset(4)] private double imag;

        // Constructors.
        public DoubleComplex(double real, double imag)
                        {
                                this.real = real;
                                this.imag = imag;
                        }

        // Addition operator.
        public static DoubleComplex operator+(DoubleComplex a, DoubleComplex b)
                        {
                                return new DoubleComplex(a.real + b.real, 
a.imag + b.imag);
                        }

        // Subtraction operator.
        public static DoubleComplex operator-(DoubleComplex a, DoubleComplex b)
                        {
                                return new DoubleComplex(a.real - b.real, 
a.imag - b.imag);
                        }

        // Multiplication operator.
        public static DoubleComplex operator*(DoubleComplex a, DoubleComplex b)
                        {
                                return new DoubleComplex
                                        (a.real * b.real - a.imag * b.imag,
                                         a.real * b.imag + a.imag * b.real);
                        }

        // Division operator.
        public static DoubleComplex operator/(DoubleComplex a, DoubleComplex b)
                        {
                                double divisor = b.real * b.real + b.imag * 
b.imag;
                                return new DoubleComplex
                                        ((a.real * b.real + a.imag * b.imag) / 
divisor,
                                         (a.imag * b.real - a.real * b.imag) / 
divisor);
                        }

        // Negation operator.
        public static DoubleComplex operator-(DoubleComplex a)
                        {
                                return new DoubleComplex(-(a.real), -(a.imag));
                        }

        // Conjugate operator.
        public static DoubleComplex operator~(DoubleComplex a)
                        {
                                return new DoubleComplex(a.real, -(a.imag));
                        }

        // Get the real and imaginary components.
        public double Real { get { return real; } }
        public double Imag { get { return imag; } }

        // Conversions.
        public static implicit operator DoubleComplex(double value)
                        {
                                return new DoubleComplex(value, 0.0);
                        }
        public static implicit operator DoubleComplex(FloatComplex value)
                        {
                                return new DoubleComplex(value.Real, 
value.Imag);
                        }
        public static implicit operator DoubleComplex(FloatImaginary value)
                        {
                                return new DoubleComplex(0.0, value.Imag);
                        }
        public static implicit operator DoubleComplex(DoubleImaginary value)
                        {
                                return new DoubleComplex(0.0, value.Imag);
                        }

} // struct DoubleComplex

[StructLayout(LayoutKind.Explicit, Size=8, Pack=8)]
[CName("double _Imaginary")]
public struct DoubleImaginary
{
        // Internal state.
        [FieldOffset(0)] private double imag;

        // Constructors.
        public DoubleImaginary(double imag)
                        {
                                this.imag = imag;
                        }

        // Get the imaginary component.
        public double Imag { get { return imag; } }

} // struct DoubleImaginary

[StructLayout(LayoutKind.Sequential)]
[CName("long double _Complex")]
public struct LongDoubleComplex
{
        // Internal state.
        private __long_double real;
        private __long_double imag;

        // Constructors.
        public LongDoubleComplex(__long_double real, __long_double imag)
                        {
                                this.real = real;
                                this.imag = imag;
                        }

        // Addition operator.
        public static LongDoubleComplex operator+
                                        (LongDoubleComplex a, LongDoubleComplex 
b)
                        {
                                return new LongDoubleComplex(a.real + b.real, 
a.imag + b.imag);
                        }

        // Subtraction operator.
        public static LongDoubleComplex operator-
                                        (LongDoubleComplex a, LongDoubleComplex 
b)
                        {
                                return new LongDoubleComplex(a.real - b.real, 
a.imag - b.imag);
                        }

        // Multiplication operator.
        public static LongDoubleComplex operator*
                                        (LongDoubleComplex a, LongDoubleComplex 
b)
                        {
                                return new LongDoubleComplex
                                        (a.real * b.real - a.imag * b.imag,
                                         a.real * b.imag + a.imag * b.real);
                        }

        // Division operator.
        public static LongDoubleComplex operator/
                                        (LongDoubleComplex a, LongDoubleComplex 
b)
                        {
                                __long_double divisor = b.real * b.real + 
b.imag * b.imag;
                                return new LongDoubleComplex
                                        ((a.real * b.real + a.imag * b.imag) / 
divisor,
                                         (a.imag * b.real - a.real * b.imag) / 
divisor);
                        }

        // Negation operator.
        public static LongDoubleComplex operator-(LongDoubleComplex a)
                        {
                                return new LongDoubleComplex(-(a.real), 
-(a.imag));
                        }

        // Conjugate operator.
        public static LongDoubleComplex operator~(LongDoubleComplex a)
                        {
                                return new LongDoubleComplex(a.real, -(a.imag));
                        }

        // Get the real and imaginary components.
        public __long_double Real { get { return real; } }
        public __long_double Imag { get { return imag; } }

        // Conversions.
        public static implicit operator LongDoubleComplex(__long_double value)
                        {
                                return new LongDoubleComplex(value, 0.0);
                        }
        public static implicit operator LongDoubleComplex(FloatComplex value)
                        {
                                return new LongDoubleComplex(value.Real, 
value.Imag);
                        }
        public static implicit operator LongDoubleComplex(DoubleComplex value)
                        {
                                return new LongDoubleComplex(value.Real, 
value.Imag);
                        }
        public static implicit operator LongDoubleComplex(FloatImaginary value)
                        {
                                return new LongDoubleComplex(0.0, value.Imag);
                        }
        public static implicit operator LongDoubleComplex(DoubleImaginary value)
                        {
                                return new LongDoubleComplex(0.0, value.Imag);
                        }
        public static implicit operator LongDoubleComplex
                                        (LongDoubleImaginary value)
                        {
                                return new LongDoubleComplex(0.0, value.Imag);
                        }

} // struct LongDoubleComplex

[StructLayout(LayoutKind.Sequential)]
[CName("long double _Imaginary")]
public struct LongDoubleImaginary
{
        // Internal state.
        private __long_double imag;

        // Constructors.
        public LongDoubleImaginary(__long_double imag)
                        {
                                this.imag = imag;
                        }

        // Get the imaginary component.
        public __long_double Imag { get { return imag; } }

} // struct LongDoubleImaginary

} // namespace OpenSystem.C

--- NEW FILE ---
/*
 * Crt0.cs - Program startup support definitions.
 *
 * This file is part of the Portable.NET "C language support" library.
 * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace OpenSystem.C
{

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Security;
using System.Reflection;

//
// The "Crt0" class contains methods that support the "crt0" code
// that the compiler outputs into modules with a "main" function.
// The "crt0" code looks something like this:
//
//              public static void .start(String[] args)
//              {
//                      try
//                      {
//                              int argc;
//                              IntPtr argv;
//                              IntPtr envp;
//                              argv = Crt0.GetArgV(args, sizeof(void *), out 
argc);
//                              envp = Crt0.GetEnvironment();
//                              Crt0.Startup("libcNN");
//                              Crt0.Shutdown(main(argc, argv, envp));
//                      }
//                      catch(OutOfMemoryException)
//                      {
//                              throw;
//                      }
//                      catch(Object e)
//                      {
//                              throw Crt0.ShutdownWithException(e);
//                      }
//              }
//
// If the "main" function does not have all three arguments, the
// compiler will only pass those arguments that "main" specifies.
//
// The "crt0" code is kept deliberately simple, with the bulk of the
// hard work done in this class.  This makes it easier to change the
// startup behaviour by altering "Crt0", without requiring every
// application to be recompiled.
//
// Note: the "crt0" code catches all exceptions, including those
// that don't inherit from "System.Exception".  Out of memory
// exceptions are explicitly excluded because there's nothing that
// can be done in that situation anyway.
//
public unsafe sealed class Crt0
{
        // Internal state.
        private static int pointerSize;
        private static IntPtr argV;
        private static int argC;
        private static IntPtr environ;
        private static Module libcModule;
        private static bool startupDone = false;

        // Get the "argv" and "argc" values for the running task.
        // "mainArgs" is the value passed to the program's entry point.
        // "ptrSize" is the size of pointers in the app's memory model.
        public static IntPtr GetArgV(String[] mainArgs, int ptrSize,
                                                                 out int argc)
                        {
                                lock(typeof(Crt0))
                                {
                                        // Bail out if we were already called 
previously.
                                        if(argV != IntPtr.Zero)
                                        {
                                                argc = argC;
                                                return argV;
                                        }

                                        // Record the pointer size for later.
                                        pointerSize = ptrSize;

                                        // Get the actual command-line 
arguments, including
                                        // the application name.  The 
application name isn't
                                        // normally included in "mainArgs", so 
we have to
                                        // do the following instead.
                                        String[] args;
                                        try
                                        {
                                                args = 
Environment.GetCommandLineArgs();
                                        }
                                        catch(NotSupportedException)
                                        {
                                                args = null;
                                        }
                                        if(args == null)
                                        {
                                                // We couldn't get the 
arguments from the runtime
                                                // engine directly, so use 
"mainArgs" and simulate
                                                // the application name.  This 
may happen in embedded
                                                // environments that don't have 
a real command-line.
                                                if(mainArgs != null)
                                                {
                                                        args = new String 
[mainArgs.Length + 1];
                                                        Array.Copy(mainArgs, 0, 
args, 1,
                                                                           
mainArgs.Length);
                                                }
                                                else
                                                {
                                                        args = new String [1];
                                                }
                                                args[0] = "cliapp.exe";
                                        }

                                        // Check that the pointer size is in 
range for the
                                        // actual platform that we are running 
on.  If not,
                                        // then it is unlikely that the 
application will work.
                                        if(ptrSize < sizeof(IntPtr))
                                        {
                                                Console.Error.Write(args[0]);
                                                Console.Error.WriteLine
                                                  (": application is {0}-bit, 
but platform is {1}-bit",
                                                   (Object)(ptrSize * 8), 
(Object)(sizeof(IntPtr) * 8));
                                                Environment.Exit(1);
                                        }

                                        // Convert "args" into an array of 
native strings,
                                        // terminated by a NULL pointer.
                                        argV = Marshal.AllocHGlobal
                                                (new IntPtr(ptrSize * 
(args.Length + 1)));
                                        if(argV == IntPtr.Zero)
                                        {
                                                // We probably don't have 
permission to allocate
                                                // memory using "AllocHGlobal". 
 If that is the
                                                // case, then bail out.  C is 
useless without it.
                                                throw new NotSupportedException
                                                        ("Fatal error: cannot 
allocate unmanaged memory");
                                        }
                                        argC = args.Length;
                                        for(int index = 0; index < argC; 
++index)
                                        {
                                                Marshal.WriteIntPtr(argV, 
ptrSize * index,
                                                        
Marshal.StringToHGlobalAnsi(args[index]));
                                        }
                                        Marshal.WriteIntPtr(argV, ptrSize * 
argC, IntPtr.Zero);

                                        // Return the final values to the 
caller.
                                        argc = argC;
                                        return argV;
                                }
                        }

        // Get the "environ" value for the running task.
        public static IntPtr GetEnvironment()
                        {
                                lock(typeof(Crt0))
                                {
                                        // Bail out if we were already called 
previously.
                                        if(environ != IntPtr.Zero)
                                        {
                                                return environ;
                                        }

                                        // Get the environment variables for 
the running task.
                                        IDictionary env;
                                        int count;
                                        IDictionaryEnumerator e;
                                        try
                                        {
                                                env = 
Environment.GetEnvironmentVariables();
                                        }
                                        catch(SecurityException)
                                        {
                                                // The runtime engine has 
decided that we don't have
                                                // sufficient permissions to 
get the environment.
                                                // We continue with an empty 
environment.
                                                env = null;
                                        }
                                        if(env != null)
                                        {
                                                count = env.Count;
                                                e = env.GetEnumerator();
                                        }
                                        else
                                        {
                                                count = 0;
                                                e = null;
                                        }

                                        // Allocate an array to hold the 
converted values.
                                        environ = Marshal.AllocHGlobal
                                                (new IntPtr(pointerSize * 
(count + 1)));

                                        // Convert the environment variables 
into native strings.
                                        int index = 0;
                                        String value;
                                        while(index < count && e != null && 
e.MoveNext())
                                        {
                                                value = 
String.Concat((String)(e.Key), "=",
                                                                                
          (String)(e.Value));
                                                Marshal.WriteIntPtr(environ, 
pointerSize * index,
                                                        
Marshal.StringToHGlobalAnsi(value));
                                                ++index;
                                        }
                                        Marshal.WriteIntPtr
                                                (environ, pointerSize * count, 
IntPtr.Zero);

                                        // The environment is ready to go.
                                        return environ;
                                }
                        }

        // Perform system library startup tasks.  This is normally
        // called just before invoking the program's "main" function.
        // "libcName" is the name of the C system library assembly,
        // which is normally "libc32" or "libc64", depending upon
        // the application's memory model.
        public static void Startup(String libcName)
                        {
                                Module mainModule;
                                Assembly assembly;
                                Type type;
                                MethodInfo method;
                                FieldInfo field;

                                // Bail out if we've already done the startup 
code.
                                lock(typeof(Crt0))
                                {
                                        if(startupDone)
                                        {
                                                return;
                                        }
                                        startupDone = true;
                                }

                                // Find the module that contains the "main" 
function.
                                mainModule = null;
                                try
                                {
                                        assembly = 
Assembly.GetCallingAssembly();
                                        type = assembly.GetType("<Module>");
                                        if(type != null)
                                        {
                                                mainModule = type.Module;
                                        }
                                }
                                catch(NotImplementedException)
                                {
                                        // The runtime engine probably does not 
have support
                                        // for reflection, so there's nothing 
we can do.
                                }

                                // Find standard C library's global module.
                                libcModule = null;
                                try
                                {
                                        assembly = Assembly.Load(libcName);
                                        type = assembly.GetType("$Module$");
                                        if(type != null)
                                        {
                                                libcModule = type.Module;
                                        }
                                }
                                catch(OutOfMemoryException)
                                {
                                        // Send out of memory conditions back 
up the stack.
                                        throw;
                                }
                                catch(Exception)
                                {
                                        // We weren't able to load "libc" for 
some reason.
                                }

                                // Set the global "__environ" variable within 
"libc".
                                if(libcModule != null)
                                {
                                        field = 
libcModule.GetField("__environ");
                                        if(field != null)
                                        {
                                                field.SetValue(null, 
(Object)environ);
                                        }
                                }

                                // Initialize the stdin, stdout, and stderr 
file descriptors.
                                FileTable.SetFileDescriptor(0, 
Console.OpenStandardInput());
                                FileTable.SetFileDescriptor(1, 
Console.OpenStandardOutput());
                                FileTable.SetFileDescriptor(2, 
Console.OpenStandardError());

                                // Invoke the application's ".init" function, 
if present.
                                if(mainModule != null)
                                {
                                        MethodInfo method = 
mainModule.GetMethod(".init");
                                        if(method != null)
                                        {
                                                method.Invoke(null, null);
                                        }
                                }

                                // Register the app's ".fini" function with 
"atexit".
                                if(mainModule != null)
                                {
                                        MethodInfo method = 
mainModule.GetMethod(".fini");
                                        if(method != null)
                                        {
                                                // TODO
                                        }
                                }
                        }

        // Perform system library shutdown tasks and exit the application.
        // This is normally called after invoking the program's "main" function.
        public static void Shutdown(int status)
                        {
                                // Find the global "exit" function and call it.
                                if(libcModule != null)
                                {
                                        MethodInfo method = 
libcModule.GetMethod("exit");
                                        if(method != null)
                                        {
                                                Object[] args = new Object [1];
                                                args[0] = (Object)(status);
                                                method.Invoke(null, args);
                                        }
                                }

                                // If we get here, then we weren't able to find 
"exit",
                                // or it returned to us by mistake.  Bail out 
through
                                // "System.Environment".
                                Environment.Exit(status);
                        }

        // Perform system library shutdown tasks when an exception occurs.
        public static Object ShutdownWithException(Object e)
                        {
                                // Nothing to do here yet, so return the 
exception object
                                // to the caller to be rethrown to the runtime 
engine.
                                return e;
                        }

        // Get the pointer size in the application's memory model.
        // Will be 4 for 32-bit memory models, and 8 for 64-bit models.
        public static int PointerSize
                        {
                                get
                                {
                                        return pointerSize;
                                }
                        }

        // Get the module that contains "libc".  Returns "null" if unknown.
        public static Module LibC
                        {
                                get
                                {
                                        return libcModule;
                                }
                        }

} // class Crt0

} // namespace OpenSystem.C

--- NEW FILE ---
/*
 * FileTable.cs - Manage the file descriptor table.
 *
 * This file is part of the Portable.NET "C language support" library.
 * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace OpenSystem.C
{

using System;
using System.IO;

//
// The file descriptor table maps integer descriptors for C
// into C# stream objects.  File descriptor functions can
// call C# stream methods to perform the required functionality.
//
public sealed class FileTable
{
        // Structure of a stream reference, which also keeps track
        // of the number of times that it has been dup'ed.
        private sealed class StreamRef
        {
                public Stream stream;
                public int count;
                public byte[] buffer;

                public StreamRef(Stream stream)
                                {
                                        this.stream = stream;
                                        this.count = 1;
                                        this.buffer = null;
                                }

        } // class StreamRef

        // Internal state.
        public const int MaxDescriptors = 256;
        private static StreamRef[] fds = new StreamRef [MaxDescriptors];

        // Set a file descriptor to a specific stream.  The previous
        // association is lost, so it should only be used on a slot
        // that is known to be empty.
        public static void SetFileDescriptor(int fd, Stream stream)
                        {
                                lock(typeof(FileTable))
                                {
                                        StreamRef[] table = fds;
                                        StreamRef sref;
                                        if(fd >= 0 && fd < MaxDescriptors)
                                        {
                                                if((sref = table[fd]) == null)
                                                {
                                                        table[fd] = new 
StreamRef(stream);
                                                }
                                                else
                                                {
                                                        sref.stream = stream;
                                                        sref.count = 1;
                                                        sref.buffer = null;
                                                }
                                        }
                                }
                        }

        // Get the stream that is associated with a file descriptor.
        // Returns "null" if there is no stream associated.
        public static Stream GetStream(int fd)
                        {
                                lock(typeof(FileTable))
                                {
                                        if(fd >= 0 && fd < MaxDescriptors)
                                        {
                                                StreamRef sref = fds[fd];
                                                if(sref != null)
                                                {
                                                        return sref.stream;
                                                }
                                                else
                                                {
                                                        return null;
                                                }
                                        }
                                        else
                                        {
                                                return null;
                                        }
                                }
                        }

        // Get the stream that is associated with a file descriptor,
        // together with a buffer that can be used for reads and writes.
        // Returns "null" if there is no stream associated.
        public static Stream GetStreamAndBuffer(int fd, out byte[] buffer)
                        {
                                lock(typeof(FileTable))
                                {
                                        if(fd >= 0 && fd < MaxDescriptors)
                                        {
                                                StreamRef sref = fds[fd];
                                                if(sref != null)
                                                {
                                                        if(sref.buffer == null)
                                                        {
                                                                sref.buffer = 
new byte [1024];
                                                        }
                                                        buffer = sref.buffer;
                                                        return sref.stream;
                                                }
                                                else
                                                {
                                                        return null;
                                                }
                                        }
                                        else
                                        {
                                                return null;
                                        }
                                }
                        }

        // Find a free file descriptor.  Returns -1 if nothing is free.
        private static int NewFD()
                        {
                                StreamRef[] table = fds;
                                int fd;
                                for(fd = 0; fd < MaxDescriptors; ++fd)
                                {
                                        if(table[fd] == null)
                                        {
                                                return fd;
                                        }
                                }
                                return -1;
                        }

        // Allocate a new file descriptor, with no stream association.
        public static int AllocFD()
                        {
                                lock(typeof(FileTable))
                                {
                                        int newFD = NewFD();
                                        if(newFD != -1)
                                        {
                                                fds[newFD] = new 
StreamRef(null);
                                        }
                                        return newFD;
                                }
                        }

        // Release a file descriptor that was allocated with "AllocFD",
        // but is no longer required due to error conditions.
        public static void ReleaseFD(int fd)
                        {
                                lock(typeof(FileTable))
                                {
                                        if(fd >= 0 && fd < MaxDescriptors)
                                        {
                                                fds[fd] = null;
                                        }
                                }
                        }

        // Duplicate a file descriptor.  Returns -1 if there are
        // no free descriptors, or -2 if the descriptor is invalid.
        public static int Dup(int fd)
                        {
                                lock(typeof(FileTable))
                                {
                                        if(fd < 0 || fd >= MaxDescriptors || 
fds[fd] == null)
                                        {
                                                return -2;
                                        }
                                        int newFd = NewFD();
                                        if(newFd == -1)
                                        {
                                                return -1;
                                        }
                                        StreamRef sref = fds[fd];
                                        ++(sref.count);
                                        fds[newFd] = sref;
                                        return newFd;
                                }
                        }

        // Duplicate a file descriptor and replace another one.
        // Returns -1 if either of the descriptors are invalid.
        public static int Dup2(int oldfd, int newfd)
                        {
                                Stream streamToClose = null;
                                lock(typeof(FileTable))
                                {
                                        StreamRef[] table = fds;
                                        if(oldfd < 0 || oldfd >= MaxDescriptors 
||
                                           table[oldfd] == null)
                                        {
                                                return -1;
                                        }
                                        if(newfd < 0 || newfd >= MaxDescriptors)
                                        {
                                                return -1;
                                        }
                                        StreamRef sref = table[oldfd];
                                        if(sref == table[newfd])
                                        {
                                                // The new stream is already 
the same as the old.
                                                return newfd;
                                        }
                                        if(table[newfd] != null)
                                        {
                                                if(--(table[newfd].count) == 0)
                                                {
                                                        streamToClose = 
table[newfd].stream;
                                                }
                                        }
                                        table[newfd] = sref;
                                        ++(sref.count);
                                }
                                if(streamToClose != null)
                                {
                                        streamToClose.Close();
                                }
                                return newfd;
                        }

        // Close a file descriptor.  Returns 0 if OK, or -1 if
        // the descriptor is invalid.
        public static int Close(int fd)
                        {
                                Stream streamToClose = null;
                                lock(typeof(FileTable))
                                {
                                        StreamRef[] table = fds;
                                        if(fd < 0 || fd >= MaxDescriptors || 
table[fd] == null)
                                        {
                                                return -1;
                                        }
                                        if(--(table[fd].count) == 0)
                                        {
                                                streamToClose = 
table[fd].stream;
                                        }
                                        table[fd] = null;
                                }
                                if(streamToClose != null)
                                {
                                        streamToClose.Close();
                                }
                                return 0;
                        }

} // class FileTable

} // namespace OpenSystem.C

--- NEW FILE ---
/*
 * LongDouble.cs - Wrapper class for the "native float" type.
 *
 * This file is part of the Portable.NET "C language support" library.
 * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace OpenSystem.C
{

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct LongDouble
{
        // Internal state.
        private __long_double value__;

        // Constructor.
        public LongDouble(__long_double value)
                        {
                                value__ = value;
                        }

        // Unpack this value to recover the "native float".
        public __long_double Unpack()
                        {
                                return value__;
                        }

} // struct LongDouble

} // namespace OpenSystem.C

--- NEW FILE ---
/*
 * LongJmp.cs - Support code for setjmp/longjmp operations.
 *
 * This file is part of the Portable.NET "C language support" library.
 * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace OpenSystem.C
{

using System;

// An exception class that is used to implement "longjmp".
public sealed class LongJmpException : Exception
{
        // Internal state.
        private int marker;
        private int value;
        private static int nextMarker = 0;

        // Constructor.
        public LongJmpException(int marker, int value)
                        : base()
                        {
                                this.marker = marker;
                                this.value = value;
                        }

        // Get the marker value from this exception object.
        public int Marker
                        {
                                get
                                {
                                        return marker;
                                }
                        }

        // Get the longjmp value from this exception object.
        public int Value
                        {
                                get
                                {
                                        return value;
                                }
                        }

        // Get a range of unique marker values for the calling method frame.
        public static int GetMarkers(int range)
                        {
                                lock(typeof(LongJmpException))
                                {
                                        int marker = nextMarker;
                                        nextMarker += range;
                                        return marker;
                                }
                        }

} // class LongJmpException

} // namespace OpenSystem.C

--- NEW FILE ---

.PHONY: OpenSystem.C.dll

all-local: OpenSystem.C.dll

OpenSystem.C.dll:
        "$(CSANT)" $(CSANT_FLAGS) -f csupport.build all

CLEANFILES = OpenSystem.C.dll

pnetassembliesdir = $(libdir)/cscc/lib
pnetassemblies_DATA = OpenSystem.C.dll

--- NEW FILE ---
/*
 * Markers.cs - Classes that mark program items for special C behaviours.
 *
 * This file is part of the Portable.NET "C language support" library.
 * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace OpenSystem.C
{

using System;

// A modifier class that is used to mark "const" types.
public sealed class IsConst {}

// A modifier class that is used to mark function pointer types.
public sealed class IsFunctionPointer {}

// An attribute class that is used to declare bit fields.
[AttributeUsage(AttributeTargets.Struct, AllowMultiple=true)]
public sealed class BitFieldAttribute : Attribute
{
        public BitFieldAttribute(String name, String storageName,
                                                         int start, int 
numBits) {}
}

// An attribute class that is used to mark weak alias definitions.
[AttributeUsage(AttributeTargets.Method)]
public sealed class WeakAliasForAttribute : Attribute
{
        public WeakAliasForAttribute(String name) {}
}

// An attribute class that is used to mark strong alias definitions.
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Method)]
public sealed class StrongAliasForAttribute : Attribute
{
        public StrongAliasForAttribute(String name) {}
}

// An attribute class that is used to mark initializers.
[AttributeUsage(AttributeTargets.Method)]
public sealed class InitializerAttribute : Attribute
{
        public InitializerAttribute() {}
}

// An attribute class that is used to specify initialization order.
[AttributeUsage(AttributeTargets.Method)]
public sealed class InitializerOrderAttribute : Attribute
{
        public InitializerOrderAttribute(int order) {}
}

// An attribute class that is used to mark finalizers.
[AttributeUsage(AttributeTargets.Method)]
public sealed class FinalizerAttribute : Attribute
{
        public FinalizerAttribute() {}
}

// An attribute class that is used to specify finalization order.
[AttributeUsage(AttributeTargets.Method)]
public sealed class FinalizerOrderAttribute : Attribute
{
        public FinalizerOrderAttribute(int order) {}
}

// An attribute class that is used to mark the memory model.
[AttributeUsage(AttributeTargets.Module)]
public sealed class MemoryModelAttribute : Attribute
{
        public MemoryModelAttribute(int ptrBits) {}
        public MemoryModelAttribute(int ptrBits, int alignFlags) {}
}

// An attribute class that is used to mark linker symbol renames.
[AttributeUsage(AttributeTargets.All)]
public sealed class OriginalNameAttribute : Attribute
{
        public OriginalNameAttribute(String name) {}
}

// An attribute class that is used to mark the C name of a type.
[AttributeUsage(AttributeTargets.Struct)]
public sealed class CNameAttribute : Attribute
{
        public CNameAttribute(String name) {}
}

} // namespace OpenSystem.C

--- NEW FILE ---

This directory contains the source code for the "OpenSystem.C.dll" assembly,
which provides support routines to the C compiler.  It is distributed
under the terms of the GNU Lesser General Public License.

Note: this isn't the standard C library (or "libc" as it is usually called).
That is defined elsewhere.  The "OpenSystem.C.dll" assembly is similar in
some respects to "libgcc.a" in the gcc compiler toolchain.

Although this isn't the standard C library, it does access functionality
from such a library.  It does this by looking for an assembly called
"libc32" or "libc64", depending upon the application's memory model.

All of the classes in "OpenSystem.C.dll" are defined within the namespace
"OpenSystem.C".  The compiler assumes that these classes exist and have
certain members.

--- NEW FILE ---
<?xml version="1.0"?>
<project name="csupport" default="all">
        <target name="all">

                <!-- Build the OpenSystem.C.dll library -->
                <compile output="OpenSystem.C.dll"
                                 target="library"
                                 unsafe="true"
                                 nostdlib="true"
                                 optimize="true"
                                 debug="true">

                        <sources>
                                <includes name="**/*.cs"/>
                        </sources>

                        <references>
                                <file name="../runtime/mscorlib.dll"/>
                        </references>

                        <arg compiler="cscc" value="-Wno-empty-input"/>
                        <arg compiler="csc" value="/nowarn:626"/>
                        <arg compiler="csc" value="/nowarn:649"/>
                        <arg compiler="csc" value="/nowarn:168"/>
                        <arg compiler="csc" value="/nowarn:67"/>
                        <arg compiler="csc" value="/nowarn:169"/>
                </compile>

        </target>
</project>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]