/** ** Changement de taille d'une zone **/ void * smMemRealloc(void *pBlock, size_t newSize) { SM_MALLOC_CHUNK *c; void *newBlock; /* get a pointer to the old block header */ c = (SM_MALLOC_CHUNK *)pBlock - 1; if (c->next != MALLOC_MAGIC) { LOGDBG(("comLib:smMemLib: realloc(something not returned by malloc)\n")); return NULL; } /* alloc new block */ newBlock = smMemMalloc(newSize); if (newBlock == NULL) { return NULL; } if (pBlock != NULL) { if (newSize > c->length) memcpy(newBlock, pBlock, c->length); else memcpy(newBlock, pBlock, newSize); smMemFree(pBlock); } return newBlock; }
H2RNG_ID h2rngCreate(int type, /* Type du ring buffer */ int nbytes) /* Nombre de bytes */ { H2RNG_ID rngId; /* Pointeur vers tete */ int flgInit; /* Flag d'initialisation */ /* Verifier si le nombre de bytes est positif */ if (nbytes <= 0) { errnoSet (S_h2rngLib_ILLEGAL_NBYTES); return ((H2RNG_ID) NULL); } /* Verifier le type demande de ring buffer */ switch (type) { case H2RNG_TYPE_BYTE: /* Ring buffer type byte */ /* Taille du ring buffer */ nbytes = nbytes + 1; /* Indiquer l'initialisation */ flgInit = H2RNG_INIT_BYTE; break; case H2RNG_TYPE_BLOCK: /* Ring buffer type "block" */ /* Taille du ring buffer */ nbytes = nbytes + 12 - (nbytes & 3); /* Indiquer l'initialisation */ flgInit = H2RNG_INIT_BLOCK; break; default: /* Types inconnus */ /* Indiquer l'erreur */ errnoSet (S_h2rngLib_ILLEGAL_TYPE); return ((H2RNG_ID) NULL); } /* switch */ /* Allouer memoire pour l'en-tete et pour le buffer */ if ((rngId = (H2RNG_ID) smMemMalloc ((size_t) (nbytes + sizeof (H2RNG_HDR)))) == NULL) { return ((H2RNG_ID) NULL); } /* Initialiser l' en-tete */ rngId->pRd = 0; rngId->pWr = 0; rngId->size = nbytes; rngId->flgInit = flgInit; /* Retourner le pointeur vers le ring buffer */ return (rngId); }
/** ** Allocation et mise a zero **/ void * smMemCalloc(size_t elemNum, size_t elemSize) { void *data; data = smMemMalloc(elemNum*elemSize); if (data == NULL) { return NULL; } memset(data, 0, elemNum*elemSize); return data; }
MSG_Q_ID msgQSmCreate ( int maxMsgs, /* max messages that can be queued */ int maxMsgLength, /* max bytes in a message */ int options /* message queue options */ ) { SM_MSG_Q_ID smMsgQId; void * pSmMsgPool; /* pointer to memory for messages */ int nodeSize = SM_MSG_NODE_SIZE (maxMsgLength); int temp; /* temp storage */ if (INT_RESTRICT () != OK) /* restrict ISR from calling */ { return (NULL); } /* * allocate shared memory message queue descriptor from * dedicated shared memory pool. */ smMsgQId = (SM_MSG_Q_ID) smMemPartAlloc ((SM_PART_ID) smMsgQPartId, sizeof (SM_MSG_Q)); if (smMsgQId == NULL) { return (NULL); } /* * allocate shared memory message queue data buffers from * shared memory system pool. */ pSmMsgPool = smMemMalloc (nodeSize * maxMsgs); if (pSmMsgPool == NULL) { smMemPartFree ((SM_PART_ID) smMsgQPartId, (char *) smMsgQId); return (NULL); } /* Initialize shared memory message queue structure */ if (msgQSmInit (smMsgQId, maxMsgs, maxMsgLength, options, pSmMsgPool) !=OK) { smMemPartFree ((SM_PART_ID) smMsgQPartId, (char *) smMsgQId); smMemFree ((char *) pSmMsgPool); return (NULL); } /* update shared infos data */ CACHE_PIPE_FLUSH (); /* CACHE FLUSH [SPR 68334] */ temp = pSmObjHdr->curNumMsgQ; /* PCI bridge bug [SPR 68844]*/ pSmObjHdr->curNumMsgQ = htonl (ntohl (pSmObjHdr->curNumMsgQ) + 1); CACHE_PIPE_FLUSH (); /* CACHE FLUSH [SPR 68334] */ temp = pSmObjHdr->curNumMsgQ; /* BRIDGE FLUSH [SPR 68334] */ return ((MSG_Q_ID) (SM_OBJ_ADRS_TO_ID (smMsgQId))); }
H2RNG_ID h2rngRealloc(H2RNG_ID rngId, /* Ring buffer identifier */ int nbytes) /* New size in bytes */ { int used; H2RNG_ID newId; /* check input validity */ if (rngId == NULL || (rngId->flgInit != H2RNG_INIT_BYTE && rngId->flgInit != H2RNG_INIT_BLOCK)) { errnoSet(S_h2rngLib_NOT_A_RING); return NULL; } if (nbytes <= 0) { errnoSet(S_h2rngLib_ILLEGAL_NBYTES); return NULL; } /* compute actual ring buffer size */ switch (rngId->flgInit) { case H2RNG_INIT_BYTE: /* Ring buffer type byte */ nbytes = nbytes + 1; break; case H2RNG_INIT_BLOCK: /* Ring buffer type "block" */ nbytes = nbytes + 12 - (nbytes & 3); break; } /* switch */ /* dummy case: same size */ if (rngId->size == nbytes) return OK; /* compute number of bytes in use */ if (rngId->pRd <= rngId->pWr) used = rngId->pWr - rngId->pRd; else used = rngId->pWr + rngId->size - rngId->pRd; /* shrinking requires special attention. The commented out code works, but * may lead to unwanted side effects for users of this API. E.g. the * resized buffer may become too small for holding some messages, and the * users of h2rngLib may not be prepared to deal with this. As long as * there is no concrete use case for shrinking h2rng buffer, be * conservative and allow only growing. */ if (rngId->size > nbytes /* && used >= nbytes */) { errnoSet(S_h2rngLib_ILLEGAL_NBYTES); return NULL; } /* allocate a new buffer */ newId = smMemMalloc((size_t)(nbytes + sizeof(H2RNG_HDR))); if (newId == NULL) return NULL; /* move current data into the new buffer */ if (rngId->pRd <= rngId->pWr) { /* one block */ memcpy((char *)(newId+1), (char *)(rngId+1) + rngId->pRd, used); } else { /* wrapping block */ int ntop = rngId->size - rngId->pRd; memcpy((char *)(newId+1), (char *)(rngId+1) + rngId->pRd, ntop); memcpy((char *)(newId+1) + ntop, (char *)(rngId+1), rngId->pWr); } /* initialize header */ newId->pRd = 0; newId->pWr = used; newId->size = nbytes; newId->flgInit = rngId->flgInit; /* free old buffer */ h2rngDelete(rngId); return newId; }