Exemplo n.º 1
0
void CMyByteArray::ReallocateDataSpace( size_t szAddition )
{
	//只有此条件才会分配空间,也即调用此函数前必须检测空间剩余
	//assert(GetSpaceLeft()<szAddition);

	//计算除去剩余空间后还应增加的空间大小
	size_t left = szAddition - GetSpaceLeft();

	//按分配粒度计算块数及剩余
	size_t nBlocks = left / GetGranularity();
	size_t cbRemain = left - nBlocks*GetGranularity();
	if(cbRemain) ++nBlocks;

	//计算新空间并重新分配
	size_t newDataSize = m_iDataSize + nBlocks*GetGranularity();
	void*  pData = (void*)new char[newDataSize];

	//复制原来的数据到新空间
	//memcpy支持第3个参数传0吗?
	if(GetSpaceUsed()) memcpy(pData,m_pData,GetSpaceUsed());

	if(m_pData) delete[] m_pData;
	m_pData = pData;
	m_iDataSize = newDataSize;
}
Exemplo n.º 2
0
static void GenerateLabel (unsigned Flags, unsigned Addr)
/* Generate a label in pass one if requested */
{
    /* Generate labels in pass #1, and only if we don't have a label already */
    if (Pass == 1 && !HaveLabel (Addr) &&
	/* Check if we must create a label */
       	((Flags & flGenLabel) != 0 ||
       	 ((Flags & flUseLabel) != 0 && Addr >= CodeStart && Addr <= CodeEnd))) {

	/* As a special case, handle ranges with tables or similar. Within
	 * such a range with a granularity > 1, do only generate dependent
	 * labels for all addresses but the first one. Be sure to generate
	 * a label for the start of the range, however.
	 */
	attr_t Style         = GetStyleAttr (Addr);
	unsigned Granularity = GetGranularity (Style);

	if (Granularity == 1) {
	    /* Just add the label */
	    AddIntLabel (Addr);
	} else {

            /* THIS CODE IS A MESS AND WILL FAIL ON SEVERAL CONDITIONS! ### */


	    /* Search for the start of the range or the last non dependent
	     * label in the range.
	     */
	    unsigned Offs;
	    attr_t LabelAttr;
	    unsigned LabelAddr = Addr;
	    while (LabelAddr > CodeStart) {

		if (Style != GetStyleAttr (LabelAddr-1)) {
		    /* End of range reached */
		    break;
		}
		--LabelAddr;
		LabelAttr = GetLabelAttr (LabelAddr);
		if ((LabelAttr & (atIntLabel|atExtLabel)) != 0) {
		    /* The address has an internal or external label */
		    break;
		}
	    }

	    /* If the proposed label address doesn't have a label, define one */
	    if ((GetLabelAttr (LabelAddr) & (atIntLabel|atExtLabel)) == 0) {
		AddIntLabel (LabelAddr);
	    }

	    /* Create the label */
	    Offs = Addr - LabelAddr;
	    if (Offs == 0) {
		AddIntLabel (Addr);
	    } else {
	     	AddDepLabel (Addr, atIntLabel, GetLabelName (LabelAddr), Offs);
	    }
	}
    }
}