summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/composite/synchronization/nativecriticalsection/resultbuffer.cpp
blob: c9ed9435f14c234f46addc2ca7a891c36e4d933e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//#include "stdafx.h"
#include "resultbuffer.h"
//
//#using <mscorlib.dll>
//
//using namespace System;


ResultBuffer:: ResultBuffer(int ThreadCount, int ThreadLogSize)
	{
		// Declare an internal status variable
		int Status=0;

		// Update the maximum thread count
		MaxThreadCount = ThreadCount;

		// Allocate the memory buffer based on the passed in thread and process counts
		// and the specified size of the thread specific buffer
		buffer = NULL;
		buffer = (char*)malloc(ThreadCount*ThreadLogSize);
		// Check to see if the buffer memory was allocated
		if (buffer == NULL)
			Status = -1;
		// Initialize the buffer to 0 to prevent bogus data
		memset(buffer,0,ThreadCount*ThreadLogSize);

		// The ThreadOffset is equal to the total number of bytes that will be stored per thread
		ThreadOffset = ThreadLogSize;

	}


		int ResultBuffer::LogResult(int Thread, char* Data)
	{
		// Declare an internal status flad
		int	status = 0;
		
		// Declare an object to store the offset address into the buffer
		int	Offset;

		// Check to make sure the  Thread index is not out of range
		if(Thread > MaxThreadCount)
        {
            printf("Thread index is out of range, Value of Thread[%d], Value of MaxThreadCount[%d]\n", Thread, MaxThreadCount);
            status = -1;
            return(status);
        }

		// Caculate the offset into the shared buffer based on the process and thread indices
		Offset = (Thread)*ThreadOffset;

		// Write the passed in data to the reserved buffer
		memcpy(buffer+Offset,Data,ThreadOffset);
        
		return(status);
	}


        char* ResultBuffer::getResultBuffer(int threadId)
        {
        
            return (buffer + threadId*ThreadOffset);    
        
        }