SimpleMemory::~SimpleMemory()
{
    size_t freedOffset = getOffset();
    size_t freedSize   = getSize();

    // keep the size to unmap in excess
    size_t pagesize = getpagesize();
    size_t start = freedOffset;
    size_t end = start + freedSize;
    start &= ~(pagesize-1);
    end = (end + pagesize-1) & ~(pagesize-1);

    // give back to the kernel the pages we don't need
    size_t free_start = freedOffset;
    size_t free_end = free_start + freedSize;
    if (start < free_start)
        start = free_start;
    if (end > free_end)
        end = free_end;
    start = (start + pagesize-1) & ~(pagesize-1);
    end &= ~(pagesize-1);    

    if (start < end) {
        void* const start_ptr = (void*)(intptr_t(getHeap()->base()) + start);
        size_t size = end-start;

#ifndef NDEBUG
        memset(start_ptr, 0xdf, size);
#endif

        // MADV_REMOVE is not defined on Dapper based Goobuntu 
#ifdef MADV_REMOVE 
        if (size) {
            int err = madvise(start_ptr, size, MADV_REMOVE);
            LOGW_IF(err, "madvise(%p, %u, MADV_REMOVE) returned %s",
                    start_ptr, size, err<0 ? strerror(errno) : "Ok");
        }
#endif
    }
}
Exemplo n.º 2
0
void fhd_candidate_selection_grid(fhd_ui* ui, int btn_width, int btn_height) {
  for (int i = 0; i < ui->fhd->candidates_len; i++) {
    fhd_texture* texture = &ui->textures[i];
    bool selected = ui->selected_candidates[i];

    void* handle = (void*)intptr_t(texture->handle);
    if (selected) {
      if (ImGui::ImageButton(
              handle, ImVec2(btn_width, btn_height), ImVec2(0, 0), ImVec2(1, 1),
              4, ImVec4(1.f, 1.f, 1.f, 1.f), ImVec4(0.f, 1.f, 0.f, 1.f))) {
        ui->selected_candidates[i] = false;
      }
    } else {
      if (ImGui::ImageButton(handle, ImVec2(btn_width, btn_height),
                             ImVec2(0, 0), ImVec2(1, 1), 2)) {
        ui->selected_candidates[i] = true;
      }
    }

    if (i % 7 < 6) ImGui::SameLine();
  }
}
Exemplo n.º 3
0
  static inline u8   get_native_u8(address p) {
    switch (intptr_t(p) & 7) {
      case 0:  return *(u8*)p;

      case 4:  return (  u8( ((u4*)p)[0] ) << 32  )
                    | (  u8( ((u4*)p)[1] )        );

      case 2:  return (  u8( ((u2*)p)[0] ) << 48  )
                    | (  u8( ((u2*)p)[1] ) << 32  )
                    | (  u8( ((u2*)p)[2] ) << 16  )
                    | (  u8( ((u2*)p)[3] )        );

     default:  return ( u8(p[0]) << 56 )
                    | ( u8(p[1]) << 48 )
                    | ( u8(p[2]) << 40 )
                    | ( u8(p[3]) << 32 )
                    | ( u8(p[4]) << 24 )
                    | ( u8(p[5]) << 16 )
                    | ( u8(p[6]) <<  8 )
                    |   u8(p[7]);
    }
  }
int gralloc_perform(struct gralloc_module_t const* module,
        int operation, ... )
{
    int res = -EINVAL;
    va_list args;
    va_start(args, operation);

    switch (operation) {
        case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: {
            int fd = va_arg(args, int);
            size_t size = va_arg(args, size_t);
            size_t offset = va_arg(args, size_t);
            void* base = va_arg(args, void*);

            // validate that it's indeed a pmem buffer
            pmem_region region;
            if (ioctl(fd, PMEM_GET_SIZE, &region) < 0) {
                break;
            }

            native_handle_t** handle = va_arg(args, native_handle_t**);
            private_handle_t* hnd = (private_handle_t*)native_handle_create(
                    private_handle_t::sNumFds, private_handle_t::sNumInts);
            hnd->magic = private_handle_t::sMagic;
            hnd->fd = fd;
            hnd->flags = private_handle_t::PRIV_FLAGS_USES_PMEM;
            hnd->size = size;
            hnd->offset = offset;
            hnd->base = intptr_t(base) + offset;
            hnd->lockState = private_handle_t::LOCK_STATE_MAPPED;
            *handle = (native_handle_t *)hnd;
            res = 0;
            break;
        }
    }

    va_end(args);
    return res;
}
SubRegionMemory::SubRegionMemory(const sp<MemoryHeapPmem>& heap,
        ssize_t offset, size_t size)
    : MemoryHeapPmem::MemoryPmem(heap), mSize(size), mOffset(offset)
{
#ifndef NDEBUG
    void* const start_ptr = (void*)(intptr_t(getHeap()->base()) + offset);
    memset(start_ptr, 0xda, size);
#endif

#ifdef HAVE_ANDROID_OS
    if (size > 0) {
        const size_t pagesize = getpagesize();
        size = (size + pagesize-1) & ~(pagesize-1);
        int our_fd = heap->heapID();
        struct pmem_region sub = { offset, size };
        int err = ioctl(our_fd, PMEM_MAP, &sub);
        ALOGE_IF(err<0, "PMEM_MAP failed (%s), "
                "mFD=%d, sub.offset=%lu, sub.size=%lu",
                strerror(errno), our_fd, sub.offset, sub.len);
}
#endif
}
Exemplo n.º 6
0
LzopStreamReader::LzopStreamReader(const void* base, size_t length)
{
	header_t			header;				// LZOP header information

	if(!base) throw Exception(E_POINTER);
	if(length == 0) throw Exception(E_INVALIDARG);

	intptr_t baseptr = intptr_t(base);

	// Verify the magic number and read the LZOP header information
	baseptr = ReadMagic(baseptr, &length);
	baseptr = ReadHeader(baseptr, &length, &header);

	// Initialize the decompression block member variables
	m_block = m_blockcurrent = NULL;
	m_blocklen = 0;
	m_blockremain = 0;

	// Initialize the LZO input stream member variables
	m_lzopos = baseptr;
	m_lzoremain = length;
	m_lzoflags = header.flags;
}
String   String::ToLower() const 
{
    uint32_t    c;
    const char* psource = GetData()->Data;
    const char* pend = psource + GetData()->GetSize();
    String      str;
    intptr_t    bufferOffset = 0;
    char        buffer[512];

    while(psource < pend)
    {
        do {
            c = UTF8Util::DecodeNextChar_Advance0(&psource);
            UTF8Util::EncodeChar(buffer, &bufferOffset, OVR_towlower(wchar_t(c)));
        } while ((psource < pend) && (bufferOffset < intptr_t(sizeof(buffer)-8)));

        // Append string a piece at a time.
        str.AppendString(buffer, bufferOffset);
        bufferOffset = 0;
    }

    return str;
}
static ANTLR3_UINT32 MemoryMapFile(pANTLR3_INPUT_STREAM input,
                                   const std::string& filename) {
  errno = 0;
  struct stat st;
  if(stat(filename.c_str(), &st) == -1) {
    return ANTLR3_ERR_NOFILE;
  }

  input->sizeBuf = st.st_size;

  int fd = open(filename.c_str(), O_RDONLY);
  if(fd == -1) {
    return ANTLR3_ERR_NOFILE;
  }

  input->data = mmap(0, input->sizeBuf, PROT_READ, MAP_PRIVATE, fd, 0);
  errno = 0;
  if(intptr_t(input->data) == -1) {
    return ANTLR3_ERR_NOMEM;
  }

  return ANTLR3_SUCCESS;
}
Exemplo n.º 9
0
static int gralloc_map(gralloc_module_t const* module,
        buffer_handle_t handle,
        void** vaddr)
{
    private_handle_t* hnd = (private_handle_t*)handle;
    if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
        size_t size = hnd->size;
#if PMEM_HACK
        size += hnd->offset;
#endif
        void* mappedAddress = mmap(0, size,
                PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
        if (mappedAddress == MAP_FAILED) {
            LOGE("Could not mmap %s", strerror(errno));
            return -errno;
        }
        hnd->base = intptr_t(mappedAddress) + hnd->offset;
        //LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p", 
        //        hnd->fd, hnd->offset, hnd->size, mappedAddress);
    }
    *vaddr = (void*)hnd->base;
    return 0;
}
Exemplo n.º 10
0
typename fixedvector<T,kmax>::iterator fixedvector<T,kmax>::iterator::operator-(intptr_t i) const// sub
{
	OrkAssert( mpfixedary );
	iterator temp( *this );
	iter_type isize = iter_type(mpfixedary->size());
	if( temp.mindex >= isize )
	{
		temp.mindex = npos;
	}
	else if( temp.mindex==npos && (i<=isize) )
	{
		temp.mindex = intptr_t(isize)-i;
	}
	else if( temp.mindex < 0 )
	{
		temp.mindex = npos;
	}
	else
	{
		temp.mindex-=i;
	}
	return temp;
}
Exemplo n.º 11
0
void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
    if (mHwc && mList) {
        result.append("Hardware Composer state:\n");

        snprintf(buffer, SIZE, "  numHwLayers=%u, flags=%08x\n",
                mList->numHwLayers, mList->flags);
        result.append(buffer);
        result.append(
                "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
                "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
        //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
        for (size_t i=0 ; i<mList->numHwLayers ; i++) {
            const hwc_layer_t& l(mList->hwLayers[i]);
            const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
            int32_t format = -1;
            if (layer->getLayer() != NULL) {
                const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
                if (buffer != NULL) {
                    format = buffer->getPixelFormat();
                }
            }
            snprintf(buffer, SIZE,
                    " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
                    l.compositionType ? "OVERLAY" : "FB",
                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
                    layer->getName().string());
            result.append(buffer);
        }
    }
    if (mHwc && mHwc->common.version >= 1 && mHwc->dump) {
        mHwc->dump(mHwc, buffer, SIZE);
        result.append(buffer);
    }
}
Exemplo n.º 12
0
c_Closure::~c_Closure() {
  // same as ar->hasThis()
  if (m_thisOrClass && !(intptr_t(m_thisOrClass) & 1LL)) {
    m_thisOrClass->decRefCount();
  }
}
Exemplo n.º 13
0
int main(int argc, char** argv) {
  glfwSetErrorCallback(glfwError);

  if (!glfwInit()) return 1;

  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  GLFWmonitor* monitor = glfwGetPrimaryMonitor();
  const GLFWvidmode* mode = glfwGetVideoMode(monitor);

  GLFWwindow* window =
      glfwCreateWindow(mode->width, mode->height, "HumanDetection", NULL, NULL);
  glfwMakeContextCurrent(window);
  glewExperimental = GL_TRUE;
  glewInit();

  fhd_context detector;
  fhd_context_init(&detector, 512, 424, 8, 8);

  ImGui_ImplGlfwGL3_Init(window, true);
  ImGui::GetStyle().WindowRounding = 0.f;
  bool show_window = true;

  fhd_ui ui(&detector);

  if (argc > 1) {
    const char* train_database = argv[1];
    ui.train_mode = true;
    fhd_candidate_db_init(&ui.candidate_db, train_database);
  }

  ImVec4 clear_color = ImColor(218, 223, 225);
  while (!glfwWindowShouldClose(window)) {
    glfwPollEvents();

    if (ImGui::IsKeyPressed(GLFW_KEY_ESCAPE)) break;

    if (ui.train_mode) {
      if (ImGui::IsKeyPressed(GLFW_KEY_X)) {
        fhd_ui_clear_candidate_selection(&ui);
        ui.depth_frame = ui.frame_source->get_frame();
      }

      if (ImGui::IsKeyPressed(GLFW_KEY_SPACE)) {
        fhd_ui_commit_candidates(&ui);
      }
    } else {
      if (ui.update_enabled) {
        ui.depth_frame = ui.frame_source->get_frame();
      }
    }

    if (ui.depth_frame) {
      auto t1 = std::chrono::high_resolution_clock::now();
      fhd_run_pass(&detector, ui.depth_frame);
      auto t2 = std::chrono::high_resolution_clock::now();
      auto duration =
          std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1);
      ui.detection_pass_time_ms = double(duration.count()) / 1000.0;
      fhd_ui_update(&ui, ui.depth_frame);
    }

    ImGui_ImplGlfwGL3_NewFrame();
    int display_w, display_h;
    glfwGetFramebufferSize(window, &display_w, &display_h);

    ImGui::SetNextWindowPos(ImVec2(0, 0));

    ImGuiWindowFlags flags = ImGuiWindowFlags_NoMove |
                             ImGuiWindowFlags_NoResize |
                             ImGuiWindowFlags_NoTitleBar;

    ImGui::Begin("foo", &show_window,
                 ImVec2(float(display_w), float(display_h)), -1.f, flags);

    ImGui::BeginChild("toolbar", ImVec2(300.f, float(display_h)));
    render_db_selection(&ui);
    render_classifier_selection(&ui);

    if (ui.train_mode) {
      ImGui::Text("*** TRAINING DB: %s ***", argv[1]);
    }

    ImGui::Text("detection pass time %.3f ms", ui.detection_pass_time_ms);
    ImGui::Text("frame source: %s", ui.database_name.c_str());
    ImGui::Text("frame %d/%d", ui.frame_source->current_frame(),
                ui.frame_source->total_frames());
    ImGui::Text("classifier: %s", ui.classifier_name.c_str());
    ImGui::Checkbox("update enabled", &ui.update_enabled);
    ImGui::SliderFloat("##det_thresh", &ui.detection_threshold, 0.f, 1.f,
                       "detection threshold %.3f");
    ImGui::InputFloat("seg k depth", &ui.fhd->depth_segmentation_threshold);
    ImGui::InputFloat("seg k normals", &ui.fhd->normal_segmentation_threshold);
    ImGui::SliderFloat("##min_reg_dim", &ui.fhd->min_region_size, 8.f, 100.f,
                       "min region dimension %.1f");
    ImGui::SliderFloat("##merge_dist_x", &ui.fhd->max_merge_distance, 0.1f, 2.f,
                       "max h merge dist (m) %.2f");
    ImGui::SliderFloat("##merge_dist_y", &ui.fhd->max_vertical_merge_distance,
                       0.1f, 3.f, "max v merge dist (m) %.2f");
    ImGui::SliderFloat("##min_inlier", &ui.fhd->min_inlier_fraction, 0.5f, 1.f,
                       "RANSAC min inlier ratio %.2f");
    ImGui::SliderFloat("##max_plane_dist", &ui.fhd->ransac_max_plane_distance,
                       0.01f, 1.f, "RANSAC max plane dist %.2f");
    ImGui::SliderFloat("##reg_height_min", &ui.fhd->min_region_height, 0.1f,
                       3.f, "min region height (m) %.2f");
    ImGui::SliderFloat("##reg_height_max", &ui.fhd->max_region_height, 0.1f,
                       3.f, "max region height (m) %.2f");
    ImGui::SliderFloat("##reg_width_min", &ui.fhd->min_region_width, 0.1f, 1.f,
                       "min region width (m) %.2f");
    ImGui::SliderFloat("##reg_width_max", &ui.fhd->max_region_height, 0.1f,
                       1.5f, "max region width (m) %.2f");
    ImGui::SliderInt("##min_depth_seg_size", &ui.fhd->min_depth_segment_size, 4,
                     200, "min depth seg size");
    ImGui::SliderInt("##min_normal_seg_size", &ui.fhd->min_normal_segment_size,
                     4, 200, "min normal seg size");
    ImGui::EndChild();

    ImGui::SameLine();

    ImGui::BeginGroup();

    ImDrawList* draw_list = ImGui::GetWindowDrawList();

    ImVec2 p = ImGui::GetCursorScreenPos();
    ImGui::Image((void*)intptr_t(ui.depth_texture.handle), ImVec2(512, 424));


    ImU32 rect_color = ImColor(240, 240, 20);
    for (int i = 0; i < detector.candidates_len; i++) {
      const fhd_candidate* candidate = &detector.candidates[i];
      if (candidate->weight >= 1.f) {
        const fhd_image_region region = candidate->depth_position;

        const float x = p.x + float(region.x);
        const float y = p.y + float(region.y);
        const float w = float(region.width);
        const float h = float(region.height);
        ImVec2 points[4] = {
          ImVec2(x, y),
          ImVec2(x + w, y),
          ImVec2(x + w, y + h),
          ImVec2(x, y + h)
        };
        draw_list->AddPolyline(points, 4, rect_color, true, 4.f, true);
      }
    }

    ImGui::BeginGroup();
    ImGui::Image((void*)intptr_t(ui.normals_texture.handle), ImVec2(256, 212));
    ImGui::SameLine();
    ImGui::Image((void*)intptr_t(ui.normals_seg_texture.handle),
                 ImVec2(256, 212));
    ImGui::EndGroup();

    ImGui::BeginGroup();
    ImGui::Image((void*)intptr_t(ui.downscaled_depth.handle), ImVec2(256, 212));
    ImGui::SameLine();
    ImGui::Image((void*)intptr_t(ui.depth_segmentation.handle),
                 ImVec2(256, 212));
    ImGui::EndGroup();

    ImGui::Image((void*)intptr_t(ui.filtered_regions.handle), ImVec2(256, 212));

    ImGui::EndGroup();

    ImGui::SameLine();

    ImGui::BeginGroup();

    if (ui.train_mode) {
      fhd_candidate_selection_grid(&ui, FHD_HOG_WIDTH * 2, FHD_HOG_HEIGHT * 2);
    } else {
      for (int i = 0; i < detector.candidates_len; i++) {
        fhd_texture* t = &ui.textures[i];
        ImGui::Image((void*)intptr_t(t->handle),
                     ImVec2(t->width * 2, t->height * 2));
        if (i % 7 < 6) ImGui::SameLine();
      }
    }

    ImGui::EndGroup();
    ImGui::End();

    glViewport(0, 0, display_w, display_h);
    glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
    glClear(GL_COLOR_BUFFER_BIT);

    ImGui::Render();
    glfwSwapBuffers(window);
  }

  if (ui.train_mode) {
    fhd_candidate_db_close(&ui.candidate_db);
  }

  fhd_texture_destroy(&ui.depth_texture);
  fhd_classifier_destroy(detector.classifier);
  ImGui_ImplGlfwGL3_Shutdown();
  glfwTerminate();

  return 0;
}
Exemplo n.º 14
0
	void ProcessLineDefs()
	{
		int sidecount = 0;
		for(unsigned i = 0, skipped = 0; i < ParsedLines.Size();)
		{
			// Relocate the vertices
			intptr_t v1i = intptr_t(ParsedLines[i].v1);
			intptr_t v2i = intptr_t(ParsedLines[i].v2);

			if (v1i >= numvertexes || v2i >= numvertexes || v1i < 0 || v2i < 0)
			{
				I_Error ("Line %d has invalid vertices: %zd and/or %zd.\nThe map only contains %d vertices.", i+skipped, v1i, v2i, numvertexes);
			}
			else if (v1i == v2i ||
				(vertexes[v1i].x == vertexes[v2i].x && vertexes[v1i].y == vertexes[v2i].y))
			{
				Printf ("Removing 0-length line %d\n", i+skipped);
				ParsedLines.Delete(i);
				ForceNodeBuild = true;
				skipped++;
			}
			else
			{
				ParsedLines[i].v1 = &vertexes[v1i];
				ParsedLines[i].v2 = &vertexes[v2i];

				if (ParsedLines[i].sidedef[0] != NULL)
					sidecount++;
				if (ParsedLines[i].sidedef[1] != NULL)
					sidecount++;
				linemap.Push(i+skipped);
				i++;
			}
		}
		numlines = ParsedLines.Size();
		numsides = sidecount;
		lines = new line_t[numlines];
		sides = new side_t[numsides];
		int line, side;

		for(line = 0, side = 0; line < numlines; line++)
		{
			short tempalpha[2] = { SHRT_MIN, SHRT_MIN };

			lines[line] = ParsedLines[line];

			for(int sd = 0; sd < 2; sd++)
			{
				if (lines[line].sidedef[sd] != NULL)
				{
					int mapside = int(intptr_t(lines[line].sidedef[sd]))-1;
					if (mapside < sidecount)
					{
						sides[side] = ParsedSides[mapside];
						sides[side].linedef = &lines[line];
						sides[side].sector = &sectors[intptr_t(sides[side].sector)];
						lines[line].sidedef[sd] = &sides[side];

						P_ProcessSideTextures(!isExtended, &sides[side], sides[side].sector, &ParsedSideTextures[mapside],
							lines[line].special, lines[line].args[0], &tempalpha[sd], missingTex);

						side++;
					}
					else
					{
						lines[line].sidedef[sd] = NULL;
					}
				}
			}

			P_AdjustLine(&lines[line]);
			P_FinishLoadingLineDef(&lines[line], tempalpha[0]);
		}
		assert(side <= numsides);
		if (side < numsides)
		{
			Printf("Map had %d invalid side references\n", numsides - side);
			numsides = side;
		}
	}
Exemplo n.º 15
0
void CNSImageProvider::cnsResponse(const unsigned char* src, int width, int height,
                                   int srcOfs, short* cns, float regVar)
{
  ASSERT(CNSResponse::SCALE == 128);

  __m128i offset = _mm_set1_epi8(static_cast<unsigned char>(CNSResponse::OFFSET));

  // Image noise of variance \c regVar increases Gauss*I^2 by 16*regVar
  // an additional factor of 16 is needed, since Gauss*I^2 is multiplied by 16
  __m128 regVarF = _mm_set1_ps(16 * 16 * regVar);

  // A pure X-gradient gives: sobelX=8, sobelY=0, gaussI=0, gaussI2=8
  // hence the fraction sobelX/sqrt(16*gaussI2-gaussI*gaussI)=1/sqrt(2)
  // The assembler code implicitly multiplies with 2^(5-16), so
  // to get the desired CNSResponse::SCALE, we multiply with
  __m128 scaleF = _mm_set1_ps(CNSResponse::SCALE / std::pow(2.f, 5.f - 16.f) * std::sqrt(2.f));

  // Buffers for intermediate values for two lines
  alignas(16) IntermediateValues iv[2][Image::maxResolutionWidth / 8]; // always 8 Pixel in one IntermediateValues object
  ASSERT((reinterpret_cast<size_t>(cns) & 0xf) == 0);

  int srcY = 0; // line in the source image

  // *** Go through two lines to fill up the intermediate Buffers
  // This is exactly the same code as below apart from the final computations being removed
  ASSERT(intptr_t(src) % 16 == 0);
  ASSERT(srcOfs % 8 == 0);
  ASSERT(width % 8 == 0);
  for(int i = 0; i < 2; ++i, ++srcY)
  {
    IntermediateValues* ivCurrent = &iv[srcY & 1][0];
    IntermediateValues* ivLast = &iv[1 - (srcY & 1)][0];
    const __m128i* pStart = reinterpret_cast<const __m128i*>(src + srcY * srcOfs - (srcY * srcOfs % 16 != 0 ? 8 : 0));
    const __m128i* pEnd = (pStart + width / 16) + (srcY * srcOfs % 16 != 0 ? 1 : 0);
    __m128i lastSrc, src;
    const __m128i* p = pStart;
    lastSrc = src = _mm_load_si128(p); //TODO change me (prev)
    for(; p != pEnd; ++ivCurrent, ++ivLast)
    {
      __m128i imgL, img, imgR;
      __m128i imgL2, img2, imgR2;
      load2x8PixelUsingSSE(imgL, img, imgR, imgL2, img2, imgR2, lastSrc, src, ++p);
      filters(*ivCurrent, *ivLast, imgL, img, imgR);
      filters(*(++ivCurrent), *(++ivLast), imgL2, img2, imgR2);
    }
  }

  // **** Now continue until the end of the image
  int yEnd = height;
  for(; srcY != yEnd; ++srcY)
  {
    IntermediateValues* ivCurrent = &iv[srcY & 1][0];
    IntermediateValues* ivLast = &iv[1 - (srcY & 1)][0];
    const __m128i* pStart = reinterpret_cast<const __m128i*>(src + srcY * srcOfs);
    const __m128i* pEnd = (pStart + width / 16);
    short* myCns = cns + (srcY - 1) * srcOfs;

    __m128i lastSrc, src;
    const __m128i* p = pStart;
    lastSrc = src = _mm_load_si128(p); //TODO change me (prev)

    for(; p < pEnd; ++ivCurrent, ++ivLast, myCns += 8)
    {
      __m128i imgL, img, imgR;
      __m128i imgL2, img2, imgR2;
      __m128i sobelX, sobelY, gaussI;
      __m128 gaussI2A, gaussI2B;
      load2x8PixelUsingSSE(imgL, img, imgR, imgL2, img2, imgR2, lastSrc, src, ++p);
      filters(*ivCurrent, *ivLast, sobelX, sobelY, gaussI, gaussI2A, gaussI2B, imgL, img, imgR);
      cnsFormula(*reinterpret_cast<__m128i*>(myCns), sobelX, sobelY, gaussI, gaussI2A, gaussI2B, scaleF, regVarF, offset);

      filters(*(++ivCurrent), *(++ivLast), sobelX, sobelY, gaussI, gaussI2A, gaussI2B, imgL2, img2, imgR2);
      cnsFormula(*reinterpret_cast<__m128i*>(myCns += 8), sobelX, sobelY, gaussI, gaussI2A, gaussI2B, scaleF, regVarF, offset);
    }

    // Left and right margin: set cns to offset (means 0) and ds to the source pixel
    myCns[-1] = myCns[-width] = static_cast<short>(static_cast<unsigned short>(CNSResponse::OFFSET + (CNSResponse::OFFSET << 8)));
  }

  // **** Finally set the top and bottom margin in the cns output if necessary
  fillWithCNSOffsetUsingSSE(cns, width);
  fillWithCNSOffsetUsingSSE(cns + (height - 1) * srcOfs, width);
}
Exemplo n.º 16
0
inline address       clear_address_bits(address x, int m)     { return address(intptr_t(x) & ~m); }
Exemplo n.º 17
0
inline address       set_address_bits(address x, int m)       { return address(intptr_t(x) | m); }
Exemplo n.º 18
0
bool ConcurrentTableSharedStore::storeImpl(const String& key,
                                           const Variant& value,
                                           int64_t ttl,
                                           bool overwrite,
                                           bool limit_ttl) {
  StoreValue *sval;
  auto svar = APCHandle::Create(value, false, APCHandleLevel::Outer, false);
  auto keyLen = key.size();
  ReadLock l(m_lock);
  char* const kcp = strdup(key.data());
  bool present;
  time_t expiry = 0;
  bool overwritePrime = false;
  {
    Map::accessor acc;
    APCHandle* current = nullptr;
    present = !m_vars.insert(acc, kcp);
    sval = &acc->second;
    if (present) {
      free(kcp);
      if (!overwrite && !sval->expired()) {
        svar.handle->unreferenceRoot(svar.size);
        return false;
      }
      sval->data.match(
        [&] (APCHandle* handle) {
          current = handle;
          // If ApcTTLLimit is set, then only primed keys can have
          // expire == 0.
          overwritePrime = sval->expire == 0;
        },
        [&] (char*) {
          // Was inFile, but won't be anymore.
          sval->data = nullptr;
          sval->dataSize = 0;
          overwritePrime = true;
        }
      );
    } else {
      APCStats::getAPCStats().addKey(keyLen);
    }

    int64_t adjustedTtl = adjust_ttl(ttl, overwritePrime || !limit_ttl);
    if (check_noTTL(key.data(), key.size())) {
      adjustedTtl = 0;
    }

    if (current) {
      if (sval->expire == 0 && adjustedTtl != 0) {
        APCStats::getAPCStats().removeAPCValue(
          sval->dataSize, current, true, sval->expired());
        APCStats::getAPCStats().addAPCValue(svar.handle, svar.size, false);
      } else {
        APCStats::getAPCStats().updateAPCValue(
          svar.handle, svar.size, current, sval->dataSize,
          sval->expire == 0, sval->expired());
      }
      current->unreferenceRoot(sval->dataSize);
    } else {
      APCStats::getAPCStats().addAPCValue(svar.handle, svar.size, present);
    }

    sval->set(svar.handle, adjustedTtl);
    sval->dataSize = svar.size;
    expiry = sval->expire;
    if (expiry) {
      auto ikey = intptr_t(acc->first);
      if (m_expMap.insert({ ikey, 0 })) {
        m_expQueue.push({ ikey, expiry });
      }
    }
  }

  if (apcExtension::ExpireOnSets) {
    purgeExpired();
  }

  return true;
}
Exemplo n.º 19
0
void BacktraceNames::FromAddr( const void *p )
{
    int fds[2];
    pid_t pid;
    pid_t ppid = getpid(); /* Do this before fork()ing! */
    
    Offset = 0;
    Address = intptr_t(p);

    if (pipe(fds) != 0)
    {
        fprintf(stderr, "FromAddr pipe() failed: %s\n", strerror(errno));
        return;
    }

    pid = fork();
    if (pid == -1)
    {
        fprintf(stderr, "FromAddr fork() failed: %s\n", strerror(errno));
        return;
    }

    if (pid == 0)
    {
        close(fds[0]);
        for (int fd = 3; fd < 1024; ++fd)
            if (fd != fds[1])
                close(fd);
        dup2(fds[1], fileno(stdout));
        close(fds[1]);

        char *addy;
        asprintf(&addy, "0x%x", long(p));
        char *p;
        asprintf(&p, "%d", ppid);

        execl("/usr/bin/atos", "/usr/bin/atos", "-p", p, addy, NULL);
        
        fprintf(stderr, "execl(atos) failed: %s\n", strerror(errno));
        free(addy);
        free(p);
        _exit(1);
    }
    
    close(fds[1]);
    char f[1024];
    bzero(f, 1024);
    int len = read(fds[0], f, 1024);

    Symbol = "";
    File = "";

    if (len == -1)
    {
        fprintf(stderr, "FromAddr read() failed: %s\n", strerror(errno));
        return;
    }
    CStringArray mangledAndFile;

    split(f, " ", mangledAndFile, true);
    if (mangledAndFile.size() == 0)
        return;
    Symbol = mangledAndFile[0];
    /* eg
     * -[NSApplication run]
     * +[SomeClass initialize]
     */
    if (Symbol[0] == '-' || Symbol[0] == '+')
    {
        Symbol = mangledAndFile[0] + " " + mangledAndFile[1];
        /* eg
         * (crt.c:300)
         * (AppKit)
         */
        if (mangledAndFile.size() == 3)
        {
            File = mangledAndFile[2];
            size_t pos = File.find('(');
            size_t start = (pos == File.npos ? 0 : pos+1);
            pos = File.rfind(')') - 1;
            File = File.substr(start, pos);
        }
        return;
    }
    /* eg
     * __start   -> _start
     * _SDL_main -> SDL_main
     */
    if (Symbol[0] == '_')
        Symbol = Symbol.substr(1);
    
    /* eg, the full line:
     * __Z1Ci (in a.out) (asmtest.cc:33)
     * _main (in a.out) (asmtest.cc:52)
     */
    if (mangledAndFile.size() > 3)
    {
        File = mangledAndFile[3];
        size_t pos = File.find('(');
        size_t start = (pos == File.npos ? 0 : pos+1);
        pos = File.rfind(')') - 1;
        File = File.substr(start, pos);
    }
    /* eg, the full line:
     * _main (SDLMain.m:308)
     * __Z8GameLoopv (crt.c:300)
     */
    else if (mangledAndFile.size() == 3)
        File = mangledAndFile[2].substr(0, mangledAndFile[2].rfind(')'));
}
Exemplo n.º 20
0
sc_synth::sc_synth(int node_id, sc_synth_definition_ptr const & prototype):
    abstract_synth(node_id, prototype)
{
    World const & world = sc_factory->world;
    const bool rt_synthesis = world.mRealTime;

    mNode.mWorld = &sc_factory->world;
    rgen.init((uint32_t)(uint64_t)this);

    /* initialize sc wrapper class */
    mRGen = &rgen;
    mSubsampleOffset = world.mSubsampleOffset;
    mSampleOffset = world.mSampleOffset;
    mLocalAudioBusUnit = 0;
    mLocalControlBusUnit = 0;

    localBufNum = 0;
    localMaxBufNum = 0;

    mNode.mID = node_id;

    sc_synthdef const & synthdef = *prototype;

    const size_t parameter_count = synthdef.parameter_count();
    const size_t constants_count = synthdef.constants.size();

    /* we allocate one memory chunk */
    const size_t wire_buffer_alignment = 64 * 8; // align to cache line boundaries
    const size_t alloc_size = prototype->memory_requirement();

    const size_t sample_alloc_size = world.mBufLength * synthdef.buffer_count
        + wire_buffer_alignment /* for alignment */;

    const size_t total_alloc_size = alloc_size + sample_alloc_size*sizeof(sample);

    char * raw_chunk = rt_synthesis ? (char*)rt_pool.malloc(total_alloc_size)
                                    : (char*)malloc(total_alloc_size);

    if (raw_chunk == nullptr)
        throw std::bad_alloc();

    linear_allocator allocator(raw_chunk);

    /* prepare controls */
    mNumControls = parameter_count;
    mControls     = allocator.alloc<float>(parameter_count);
    mControlRates = allocator.alloc<int>(parameter_count);
    mMapControls  = allocator.alloc<float*>(parameter_count);

    /* initialize controls */
    for (size_t i = 0; i != parameter_count; ++i) {
        mControls[i] = synthdef.parameters[i]; /* initial parameters */
        mMapControls[i] = &mControls[i];       /* map to control values */
        mControlRates[i] = 0;                  /* init to 0*/
    }

    /* allocate constant wires */
    mWire = allocator.alloc<Wire>(constants_count);
    for (size_t i = 0; i != synthdef.constants.size(); ++i) {
        Wire * wire = mWire + i;
        wire->mFromUnit = 0;
        wire->mCalcRate = 0;
        wire->mBuffer = 0;
        wire->mScalarValue = get_constant(i);
    }

    unit_count = prototype->unit_count();
    calc_unit_count = prototype->calc_unit_count();
    units        = allocator.alloc<Unit*>(unit_count);
    calc_units   = allocator.alloc<Unit*>(calc_unit_count + 1); // over-allocate to allow prefetching
    unit_buffers = allocator.alloc<sample>(sample_alloc_size);

    const int alignment_mask = wire_buffer_alignment - 1;
    unit_buffers = (sample*) ((intptr_t(unit_buffers) + alignment_mask) & ~alignment_mask);     /* next aligned pointer */

    /* allocate unit generators */
    sc_factory->allocate_ugens(synthdef.graph.size());
    for (size_t i = 0; i != synthdef.graph.size(); ++i) {
        sc_synthdef::unit_spec_t const & spec = synthdef.graph[i];
        units[i] = spec.prototype->construct(spec, this, &sc_factory->world, allocator);
    }

    for (size_t i = 0; i != synthdef.calc_unit_indices.size(); ++i) {
        int32_t index = synthdef.calc_unit_indices[i];
        calc_units[i] = units[index];
    }

    assert((char*)mControls + alloc_size <= allocator.alloc<char>()); // ensure the memory boundaries
}
Exemplo n.º 21
0
MOZ_ASAN_BLACKLIST void
ThreadStackHelper::FillThreadContext(void* aContext)
{
#ifdef MOZ_THREADSTACKHELPER_NATIVE
  if (!mContextToFill) {
    return;
  }

#if 0 // TODO: remove dependency on Breakpad structs.
#if defined(XP_LINUX)
  const ucontext_t& context = *reinterpret_cast<ucontext_t*>(aContext);
#if defined(MOZ_THREADSTACKHELPER_X86)
  mContextToFill->mContext.context_flags = MD_CONTEXT_X86_FULL;
  mContextToFill->mContext.edi = context.uc_mcontext.gregs[REG_EDI];
  mContextToFill->mContext.esi = context.uc_mcontext.gregs[REG_ESI];
  mContextToFill->mContext.ebx = context.uc_mcontext.gregs[REG_EBX];
  mContextToFill->mContext.edx = context.uc_mcontext.gregs[REG_EDX];
  mContextToFill->mContext.ecx = context.uc_mcontext.gregs[REG_ECX];
  mContextToFill->mContext.eax = context.uc_mcontext.gregs[REG_EAX];
  mContextToFill->mContext.ebp = context.uc_mcontext.gregs[REG_EBP];
  mContextToFill->mContext.eip = context.uc_mcontext.gregs[REG_EIP];
  mContextToFill->mContext.eflags = context.uc_mcontext.gregs[REG_EFL];
  mContextToFill->mContext.esp = context.uc_mcontext.gregs[REG_ESP];
#elif defined(MOZ_THREADSTACKHELPER_X64)
  mContextToFill->mContext.context_flags = MD_CONTEXT_AMD64_FULL;
  mContextToFill->mContext.eflags = uint32_t(context.uc_mcontext.gregs[REG_EFL]);
  mContextToFill->mContext.rax = context.uc_mcontext.gregs[REG_RAX];
  mContextToFill->mContext.rcx = context.uc_mcontext.gregs[REG_RCX];
  mContextToFill->mContext.rdx = context.uc_mcontext.gregs[REG_RDX];
  mContextToFill->mContext.rbx = context.uc_mcontext.gregs[REG_RBX];
  mContextToFill->mContext.rsp = context.uc_mcontext.gregs[REG_RSP];
  mContextToFill->mContext.rbp = context.uc_mcontext.gregs[REG_RBP];
  mContextToFill->mContext.rsi = context.uc_mcontext.gregs[REG_RSI];
  mContextToFill->mContext.rdi = context.uc_mcontext.gregs[REG_RDI];
  memcpy(&mContextToFill->mContext.r8,
         &context.uc_mcontext.gregs[REG_R8], 8 * sizeof(int64_t));
  mContextToFill->mContext.rip = context.uc_mcontext.gregs[REG_RIP];
#elif defined(MOZ_THREADSTACKHELPER_ARM)
  mContextToFill->mContext.context_flags = MD_CONTEXT_ARM_FULL;
  memcpy(&mContextToFill->mContext.iregs[0],
         &context.uc_mcontext.arm_r0, 17 * sizeof(int32_t));
#else
  #error "Unsupported architecture"
#endif // architecture

#elif defined(XP_WIN)
  // Breakpad context struct is based off of the Windows CONTEXT struct,
  // so we assume they are the same; do some sanity checks to make sure.
  static_assert(sizeof(ThreadContext::Context) == sizeof(::CONTEXT),
                "Context struct mismatch");
  static_assert(offsetof(ThreadContext::Context, context_flags) ==
                offsetof(::CONTEXT, ContextFlags),
                "Context struct mismatch");
  mContextToFill->mContext.context_flags = CONTEXT_FULL;
  NS_ENSURE_TRUE_VOID(::GetThreadContext(mThreadID,
      reinterpret_cast<::CONTEXT*>(&mContextToFill->mContext)));

#elif defined(XP_MACOSX)
#if defined(MOZ_THREADSTACKHELPER_X86)
  const thread_state_flavor_t flavor = x86_THREAD_STATE32;
  x86_thread_state32_t state = {};
  mach_msg_type_number_t count = x86_THREAD_STATE32_COUNT;
#elif defined(MOZ_THREADSTACKHELPER_X64)
  const thread_state_flavor_t flavor = x86_THREAD_STATE64;
  x86_thread_state64_t state = {};
  mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT;
#elif defined(MOZ_THREADSTACKHELPER_ARM)
  const thread_state_flavor_t flavor = ARM_THREAD_STATE;
  arm_thread_state_t state = {};
  mach_msg_type_number_t count = ARM_THREAD_STATE_COUNT;
#endif
  NS_ENSURE_TRUE_VOID(KERN_SUCCESS == ::thread_get_state(
    mThreadID, flavor, reinterpret_cast<thread_state_t>(&state), &count));
#if __DARWIN_UNIX03
#define GET_REGISTER(s, r) ((s).__##r)
#else
#define GET_REGISTER(s, r) ((s).r)
#endif
#if defined(MOZ_THREADSTACKHELPER_X86)
  mContextToFill->mContext.context_flags = MD_CONTEXT_X86_FULL;
  mContextToFill->mContext.edi = GET_REGISTER(state, edi);
  mContextToFill->mContext.esi = GET_REGISTER(state, esi);
  mContextToFill->mContext.ebx = GET_REGISTER(state, ebx);
  mContextToFill->mContext.edx = GET_REGISTER(state, edx);
  mContextToFill->mContext.ecx = GET_REGISTER(state, ecx);
  mContextToFill->mContext.eax = GET_REGISTER(state, eax);
  mContextToFill->mContext.ebp = GET_REGISTER(state, ebp);
  mContextToFill->mContext.eip = GET_REGISTER(state, eip);
  mContextToFill->mContext.eflags = GET_REGISTER(state, eflags);
  mContextToFill->mContext.esp = GET_REGISTER(state, esp);
#elif defined(MOZ_THREADSTACKHELPER_X64)
  mContextToFill->mContext.context_flags = MD_CONTEXT_AMD64_FULL;
  mContextToFill->mContext.eflags = uint32_t(GET_REGISTER(state, rflags));
  mContextToFill->mContext.rax = GET_REGISTER(state, rax);
  mContextToFill->mContext.rcx = GET_REGISTER(state, rcx);
  mContextToFill->mContext.rdx = GET_REGISTER(state, rdx);
  mContextToFill->mContext.rbx = GET_REGISTER(state, rbx);
  mContextToFill->mContext.rsp = GET_REGISTER(state, rsp);
  mContextToFill->mContext.rbp = GET_REGISTER(state, rbp);
  mContextToFill->mContext.rsi = GET_REGISTER(state, rsi);
  mContextToFill->mContext.rdi = GET_REGISTER(state, rdi);
  memcpy(&mContextToFill->mContext.r8,
         &GET_REGISTER(state, r8), 8 * sizeof(int64_t));
  mContextToFill->mContext.rip = GET_REGISTER(state, rip);
#elif defined(MOZ_THREADSTACKHELPER_ARM)
  mContextToFill->mContext.context_flags = MD_CONTEXT_ARM_FULL;
  memcpy(mContextToFill->mContext.iregs,
         GET_REGISTER(state, r), 17 * sizeof(int32_t));
#else
  #error "Unsupported architecture"
#endif // architecture
#undef GET_REGISTER

#else
  #error "Unsupported platform"
#endif // platform

  intptr_t sp = 0;
#if defined(MOZ_THREADSTACKHELPER_X86)
  sp = mContextToFill->mContext.esp;
#elif defined(MOZ_THREADSTACKHELPER_X64)
  sp = mContextToFill->mContext.rsp;
#elif defined(MOZ_THREADSTACKHELPER_ARM)
  sp = mContextToFill->mContext.iregs[13];
#else
  #error "Unsupported architecture"
#endif // architecture
  NS_ENSURE_TRUE_VOID(sp);
  NS_ENSURE_TRUE_VOID(mThreadStackBase);

  size_t stackSize = std::min(intptr_t(ThreadContext::kMaxStackSize),
                              std::abs(sp - mThreadStackBase));

  if (mContextToFill->mStackEnd) {
    // Limit the start of stack to a certain location if specified.
    stackSize = std::min(intptr_t(stackSize),
      std::abs(sp - intptr_t(mContextToFill->mStackEnd)));
  }

#ifndef MOZ_THREADSTACKHELPER_STACK_GROWS_DOWN
  // If if the stack grows upwards, and we need to recalculate our
  // stack copy's base address. Subtract sizeof(void*) so that the
  // location pointed to by sp is included.
  sp -= stackSize - sizeof(void*);
#endif

#ifndef MOZ_ASAN
  memcpy(mContextToFill->mStack.get(), reinterpret_cast<void*>(sp), stackSize);
  // Valgrind doesn't care about the access outside the stack frame, but
  // the presence of uninitialised values on the stack does cause it to
  // later report a lot of false errors when Breakpad comes to unwind it.
  // So mark the extracted data as defined.
  MOZ_MAKE_MEM_DEFINED(mContextToFill->mStack.get(), stackSize);
#else
  // ASan will flag memcpy for access outside of stack frames,
  // so roll our own memcpy here.
  intptr_t* dst = reinterpret_cast<intptr_t*>(&mContextToFill->mStack[0]);
  const intptr_t* src = reinterpret_cast<intptr_t*>(sp);
  for (intptr_t len = stackSize; len > 0; len -= sizeof(*src)) {
    *(dst++) = *(src++);
  }
#endif

  mContextToFill->mStackBase = uintptr_t(sp);
  mContextToFill->mStackSize = stackSize;
  mContextToFill->mValid = true;
#endif
#endif // MOZ_THREADSTACKHELPER_NATIVE
}
Exemplo n.º 22
0
void ParGCAllocBufferWithBOT::retire(bool end_of_gc, bool retain) {
  assert(!retain || end_of_gc, "Can only retain at GC end.");
  if (_retained) {
    // We're about to make the retained_filler into a block.
    _bt.BlockOffsetArray::alloc_block(_retained_filler.start(),
                                      _retained_filler.end());
  }
  // Reset _hard_end to _true_end (and update _end)
  if (retain && _hard_end != NULL) {
    assert(_hard_end <= _true_end, "Invariant.");
    _hard_end = _true_end;
    _end      = MAX2(_top, _hard_end - AlignmentReserve);
    assert(_end <= _hard_end, "Invariant.");
  }
  _true_end = _hard_end;
  HeapWord* pre_top = _top;

  ParGCAllocBuffer::retire(end_of_gc, retain);
  // Now any old _retained_filler is cut back to size, the free part is
  // filled with a filler object, and top is past the header of that
  // object.

  if (retain && _top < _end) {
    assert(end_of_gc && retain, "Or else retain should be false.");
    // If the lab does not start on a card boundary, we don't want to
    // allocate onto that card, since that might lead to concurrent
    // allocation and card scanning, which we don't support.  So we fill
    // the first card with a garbage object.
    size_t first_card_index = _bsa->index_for(pre_top);
    HeapWord* first_card_start = _bsa->address_for_index(first_card_index);
    if (first_card_start < pre_top) {
      HeapWord* second_card_start =
        _bsa->inc_by_region_size(first_card_start);

      // Ensure enough room to fill with the smallest block
      second_card_start = MAX2(second_card_start, pre_top + AlignmentReserve);

      // If the end is already in the first card, don't go beyond it!
      // Or if the remainder is too small for a filler object, gobble it up.
      if (_hard_end < second_card_start ||
          pointer_delta(_hard_end, second_card_start) < AlignmentReserve) {
        second_card_start = _hard_end;
      }
      if (pre_top < second_card_start) {
        MemRegion first_card_suffix(pre_top, second_card_start);
        fill_region_with_block(first_card_suffix, true);
      }
      pre_top = second_card_start;
      _top = pre_top;
      _end = MAX2(_top, _hard_end - AlignmentReserve);
    }

    // If the lab does not end on a card boundary, we don't want to
    // allocate onto that card, since that might lead to concurrent
    // allocation and card scanning, which we don't support.  So we fill
    // the last card with a garbage object.
    size_t last_card_index = _bsa->index_for(_hard_end);
    HeapWord* last_card_start = _bsa->address_for_index(last_card_index);
    if (last_card_start < _hard_end) {

      // Ensure enough room to fill with the smallest block
      last_card_start = MIN2(last_card_start, _hard_end - AlignmentReserve);

      // If the top is already in the last card, don't go back beyond it!
      // Or if the remainder is too small for a filler object, gobble it up.
      if (_top > last_card_start ||
          pointer_delta(last_card_start, _top) < AlignmentReserve) {
        last_card_start = _top;
      }
      if (last_card_start < _hard_end) {
        MemRegion last_card_prefix(last_card_start, _hard_end);
        fill_region_with_block(last_card_prefix, false);
      }
      _hard_end = last_card_start;
      _end      = MAX2(_top, _hard_end - AlignmentReserve);
      _true_end = _hard_end;
      assert(_end <= _hard_end, "Invariant.");
    }

    // At this point:
    //   1) we had a filler object from the original top to hard_end.
    //   2) We've filled in any partial cards at the front and back.
    if (pre_top < _hard_end) {
      // Now we can reset the _bt to do allocation in the given area.
      MemRegion new_filler(pre_top, _hard_end);
      fill_region_with_block(new_filler, false);
      _top = pre_top + ParGCAllocBuffer::FillerHeaderSize;
      // If there's no space left, don't retain.
      if (_top >= _end) {
        _retained = false;
        invalidate();
        return;
      }
      _retained_filler = MemRegion(pre_top, _top);
      _bt.set_region(MemRegion(_top, _hard_end));
      _bt.initialize_threshold();
      assert(_bt.threshold() > _top, "initialize_threshold failed!");

      // There may be other reasons for queries into the middle of the
      // filler object.  When such queries are done in parallel with
      // allocation, bad things can happen, if the query involves object
      // iteration.  So we ensure that such queries do not involve object
      // iteration, by putting another filler object on the boundaries of
      // such queries.  One such is the object spanning a parallel card
      // chunk boundary.

      // "chunk_boundary" is the address of the first chunk boundary less
      // than "hard_end".
      HeapWord* chunk_boundary =
        (HeapWord*)align_size_down(intptr_t(_hard_end-1), ChunkSizeInBytes);
      assert(chunk_boundary < _hard_end, "Or else above did not work.");
      assert(pointer_delta(_true_end, chunk_boundary) >= AlignmentReserve,
             "Consequence of last card handling above.");

      if (_top <= chunk_boundary) {
        assert(_true_end == _hard_end, "Invariant.");
        while (_top <= chunk_boundary) {
          assert(pointer_delta(_hard_end, chunk_boundary) >= AlignmentReserve,
                 "Consequence of last card handling above.");
          _bt.BlockOffsetArray::alloc_block(chunk_boundary, _hard_end);
          CollectedHeap::fill_with_object(chunk_boundary, _hard_end);
          _hard_end = chunk_boundary;
          chunk_boundary -= ChunkSizeInWords;
        }
        _end = _hard_end - AlignmentReserve;
        assert(_top <= _end, "Invariant.");
        // Now reset the initial filler chunk so it doesn't overlap with
        // the one(s) inserted above.
        MemRegion new_filler(pre_top, _hard_end);
        fill_region_with_block(new_filler, false);
      }
    } else {
      _retained = false;
      invalidate();
    }
  } else {
    assert(!end_of_gc ||
           (!_retained && _true_end == _hard_end), "Checking.");
  }
  assert(_end <= _hard_end, "Invariant.");
  assert(_top < _end || _top == _hard_end, "Invariant");
}
Exemplo n.º 23
0
static void LoadWalls (walltype *walls, int numwalls, sectortype *bsec)
{
    int i, j;

    // Setting numvertexes to the same as numwalls is overly conservative,
    // but the extra vertices will be removed during the BSP building pass.
    numsides = numvertexes = numwalls;
    numlines = 0;

    sides = new side_t[numsides];
    memset (sides, 0, numsides*sizeof(side_t));

    vertexes = new vertex_t[numvertexes];
    numvertexes = 0;

    // First mark each sidedef with the sector it belongs to
    for (i = 0; i < numsectors; ++i)
    {
        if (bsec[i].wallptr >= 0)
        {
            for (j = 0; j < bsec[i].wallnum; ++j)
            {
                sides[j + bsec[i].wallptr].sector = sectors + i;
            }
        }
    }

    // Now copy wall properties to their matching sidedefs
    for (i = 0; i < numwalls; ++i)
    {
        char tnam[9];
        FTextureID overpic, pic;

        mysnprintf (tnam, countof(tnam), "BTIL%04d", LittleShort(walls[i].picnum));
        pic = TexMan.GetTexture (tnam, FTexture::TEX_Build);
        mysnprintf (tnam, countof(tnam), "BTIL%04d", LittleShort(walls[i].overpicnum));
        overpic = TexMan.GetTexture (tnam, FTexture::TEX_Build);

        walls[i].x = LittleLong(walls[i].x);
        walls[i].y = LittleLong(walls[i].y);
        walls[i].point2 = LittleShort(walls[i].point2);
        walls[i].cstat = LittleShort(walls[i].cstat);
        walls[i].nextwall = LittleShort(walls[i].nextwall);
        walls[i].nextsector = LittleShort(walls[i].nextsector);

        sides[i].SetTextureXOffset(walls[i].xpanning << FRACBITS);
        sides[i].SetTextureYOffset(walls[i].ypanning << FRACBITS);

        sides[i].SetTexture(side_t::top, pic);
        sides[i].SetTexture(side_t::bottom, pic);
        if (walls[i].nextsector < 0 || (walls[i].cstat & 32))
        {
            sides[i].SetTexture(side_t::mid, pic);
        }
        else if (walls[i].cstat & 16)
        {
            sides[i].SetTexture(side_t::mid, overpic);
        }
        else
        {
            sides[i].SetTexture(side_t::mid, FNullTextureID());
        }

        sides[i].TexelLength = walls[i].xrepeat * 8;
        sides[i].SetTextureYScale(walls[i].yrepeat << (FRACBITS - 3));
        sides[i].SetTextureXScale(FRACUNIT);
        sides[i].SetLight(SHADE2LIGHT(walls[i].shade));
        sides[i].Flags = WALLF_ABSLIGHTING;
        sides[i].RightSide = walls[i].point2;
        sides[walls[i].point2].LeftSide = i;

        if (walls[i].nextwall >= 0 && walls[i].nextwall <= i)
        {
            sides[i].linedef = sides[walls[i].nextwall].linedef;
        }
        else
        {
            sides[i].linedef = (line_t*)(intptr_t)(numlines++);
        }
    }

    // Set line properties that Doom doesn't store per-sidedef
    lines = new line_t[numlines];
    memset (lines, 0, numlines*sizeof(line_t));

    for (i = 0, j = -1; i < numwalls; ++i)
    {
        if (walls[i].nextwall >= 0 && walls[i].nextwall <= i)
        {
            continue;
        }

        j = int(intptr_t(sides[i].linedef));
        lines[j].sidedef[0] = (side_t*)(intptr_t)i;
        lines[j].sidedef[1] = (side_t*)(intptr_t)walls[i].nextwall;
        lines[j].v1 = FindVertex (walls[i].x, walls[i].y);
        lines[j].v2 = FindVertex (walls[walls[i].point2].x, walls[walls[i].point2].y);
        lines[j].frontsector = sides[i].sector;
        lines[j].flags |= ML_WRAP_MIDTEX;
        if (walls[i].nextsector >= 0)
        {
            lines[j].backsector = sectors + walls[i].nextsector;
            lines[j].flags |= ML_TWOSIDED;
        }
        else
        {
            lines[j].backsector = NULL;
        }
        P_AdjustLine (&lines[j]);
        if (walls[i].cstat & 128)
        {
            if (walls[i].cstat & 512)
            {
                lines[j].Alpha = FRACUNIT/3;
            }
            else
            {
                lines[j].Alpha = FRACUNIT*2/3;
            }
        }
        if (walls[i].cstat & 1)
        {
            lines[j].flags |= ML_BLOCKING;
        }
        if (walls[i].nextwall < 0)
        {
            if (walls[i].cstat & 4)
            {
                lines[j].flags |= ML_DONTPEGBOTTOM;
            }
        }
        else
        {
            if (walls[i].cstat & 4)
            {
                lines[j].flags |= ML_DONTPEGTOP;
            }
            else
            {
                lines[j].flags |= ML_DONTPEGBOTTOM;
            }
        }
        if (walls[i].cstat & 64)
        {
            lines[j].flags |= ML_BLOCKEVERYTHING;
        }
    }

    // Finish setting sector properties that depend on walls
    for (i = 0; i < numsectors; ++i, ++bsec)
    {
        SlopeWork slope;

        slope.wal = &walls[bsec->wallptr];
        slope.wal2 = &walls[slope.wal->point2];
        slope.dx = slope.wal2->x - slope.wal->x;
        slope.dy = slope.wal2->y - slope.wal->y;
        slope.i = long (sqrt ((double)(slope.dx*slope.dx+slope.dy*slope.dy))) << 5;
        if (slope.i == 0)
        {
            continue;
        }
        if ((bsec->floorstat & 2) && (bsec->floorheinum != 0))
        {   // floor is sloped
            slope.heinum = -LittleShort(bsec->floorheinum);
            slope.z[0] = slope.z[1] = slope.z[2] = -bsec->floorz;
            CalcPlane (slope, sectors[i].floorplane);
        }
        if ((bsec->ceilingstat & 2) && (bsec->ceilingheinum != 0))
        {   // ceiling is sloped
            slope.heinum = -LittleShort(bsec->ceilingheinum);
            slope.z[0] = slope.z[1] = slope.z[2] = -bsec->ceilingz;
            CalcPlane (slope, sectors[i].ceilingplane);
        }
        int linenum = int(intptr_t(sides[bsec->wallptr].linedef));
        int sidenum = int(intptr_t(lines[linenum].sidedef[1]));
        if (bsec->floorstat & 64)
        {   // floor is aligned to first wall
            P_AlignFlat (linenum, sidenum == bsec->wallptr, 0);
        }
        if (bsec->ceilingstat & 64)
        {   // ceiling is aligned to first wall
            P_AlignFlat (linenum, sidenum == bsec->wallptr, 0);
        }
    }
    for (i = 0; i < numlines; i++)
    {
        intptr_t front = intptr_t(lines[i].sidedef[0]);
        intptr_t back = intptr_t(lines[i].sidedef[1]);
        lines[i].sidedef[0] = front >= 0 ? &sides[front] : NULL;
        lines[i].sidedef[1] = back >= 0 ? &sides[back] : NULL;
    }
    for (i = 0; i < numsides; i++)
    {
        assert(sides[i].sector != NULL);
        sides[i].linedef = &lines[intptr_t(sides[i].linedef)];
    }
}
Exemplo n.º 24
0
 static inline u2   get_native_u2(address p){
   return (intptr_t(p) & 1) == 0
            ?   *(u2*)p
            :   ( u2(p[1]) << 8 )
              | ( u2(p[0])      );
 }
Exemplo n.º 25
0
bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
// QQQ
#ifdef CC_INTERP
#else
  assert(is_interpreted_frame(), "Not an interpreted frame");
  // These are reasonable sanity checks
  if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
    return false;
  }
  if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
    return false;
  }
  if (fp() + interpreter_frame_initial_sp_offset < sp()) {
    return false;
  }
  // These are hacks to keep us out of trouble.
  // The problem with these is that they mask other problems
  if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
    return false;
  }

  // do some validation of frame elements

  // first the method

  methodOop m = *interpreter_frame_method_addr();

  // validate the method we'd find in this potential sender
  if (!Universe::heap()->is_valid_method(m)) return false;

  // stack frames shouldn't be much larger than max_stack elements

  if (fp() - sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
    return false;
  }

  // validate bci/bcx

  intptr_t  bcx    = interpreter_frame_bcx();
  if (m->validate_bci_from_bcx(bcx) < 0) {
    return false;
  }

  // validate constantPoolCacheOop

  constantPoolCacheOop cp = *interpreter_frame_cache_addr();

  if (cp == NULL ||
      !Space::is_aligned(cp) ||
      !Universe::heap()->is_permanent((void*)cp)) return false;

  // validate locals

  address locals =  (address) *interpreter_frame_locals_addr();

  if (locals > thread->stack_base() || locals < (address) fp()) return false;

  // We'd have to be pretty unlucky to be mislead at this point

#endif // CC_INTERP
  return true;
}
Exemplo n.º 26
0
Arquivo: vm.c Projeto: Ododo/etlegacy
/*
 * Reload the data, but leave everything else in place
 * This allows a server to do a map_restart without changing memory allocation
 */
vm_t *VM_Restart(vm_t *vm)
{
	vmHeader_t *header;
	int        dataLength;
	int        i;
	char       filename[MAX_QPATH];

	// DLL's can't be restarted in place
	if (vm->dllHandle)
	{
		char     name[MAX_QPATH];
		intptr_t (*systemCall)(intptr_t *parms);

		systemCall = vm->systemCall;
		Q_strncpyz(name, vm->name, sizeof(name));

		VM_Free(vm);

		vm = VM_Create(name, systemCall, VMI_NATIVE);
		return vm;
	}

	// load the image
	Com_Printf("VM_Restart()\n");
	Com_sprintf(filename, sizeof(filename), "vm/%s.qvm", vm->name);
	Com_Printf("Loading vm file %s.\n", filename);
	FS_ReadFile(filename, (void **)&header);
	if (!header)
	{
		Com_Error(ERR_DROP, "VM_Restart: restart failed.");
	}

	// byte swap the header
	for (i = 0; i < sizeof(*header) / 4; i++)
	{
		((int *)header)[i] = LittleLong(((int *)header)[i]);
	}

	// validate
	if (header->vmMagic != VM_MAGIC
	    || header->bssLength < 0 || header->dataLength < 0 || header->litLength < 0 || header->codeLength <= 0)
	{
		VM_Free(vm);
		Com_Error(ERR_FATAL, "VM_Restart: %s has bad header", filename);
	}

	// round up to next power of 2 so all data operations can
	// be mask protected
	dataLength = header->dataLength + header->litLength + header->bssLength;
	for (i = 0; dataLength > (1 << i); i++)
	{
	}
	dataLength = 1 << i;

	// clear the data
	Com_Memset(vm->dataBase, 0, dataLength);

	// copy the intialized data
	Com_Memcpy(vm->dataBase, (byte *) header + header->dataOffset, header->dataLength + header->litLength);

	// byte swap the longs
	for (i = 0; i < header->dataLength; i += 4)
	{
		*(int *)(vm->dataBase + i) = LittleLong(*(int *)(vm->dataBase + i));
	}

	// free the original file
	FS_FreeFile(header);

	return vm;
}
Exemplo n.º 27
0
void GLReplay::RenderMesh(int frameID, vector<int> eventID, MeshDisplay cfg)
{
	WrappedOpenGL &gl = *m_pDriver;
	
	MakeCurrentReplayContext(m_DebugCtx);
	
	GLuint curFBO = 0;
	gl.glGetIntegerv(eGL_FRAMEBUFFER_BINDING, (GLint*)&curFBO);

	OutputWindow *outw = NULL;
	for(auto it = m_OutputWindows.begin(); it != m_OutputWindows.end(); ++it)
	{
		if(it->second.BlitData.windowFBO == curFBO)
		{
			outw = &it->second;
			break;
		}
	}

	if(!outw) return;
	
	const auto &attr = m_CurPipelineState.m_VtxIn.attributes[0];
	const auto &vb = m_CurPipelineState.m_VtxIn.vbuffers[attr.BufferSlot];

	if(vb.Buffer == ResourceId())
		return;
	
	MakeCurrentReplayContext(&m_ReplayCtx);

	GLint viewport[4];
	gl.glGetIntegerv(eGL_VIEWPORT, viewport);
	
	gl.glGetIntegerv(eGL_FRAMEBUFFER_BINDING, (GLint*)&curFBO);

	if(outw->BlitData.replayFBO == 0)
	{
		gl.glGenFramebuffers(1, &outw->BlitData.replayFBO);
		gl.glBindFramebuffer(eGL_FRAMEBUFFER, outw->BlitData.replayFBO);

		gl.glFramebufferTexture(eGL_FRAMEBUFFER, eGL_COLOR_ATTACHMENT0, outw->BlitData.backbuffer, 0);
	}
	else
	{
		gl.glBindFramebuffer(eGL_FRAMEBUFFER, outw->BlitData.replayFBO);
	}
	
	gl.glViewport(0, 0, (GLsizei)DebugData.outWidth, (GLsizei)DebugData.outHeight);
	
	GLuint curProg = 0;
	gl.glGetIntegerv(eGL_CURRENT_PROGRAM, (GLint*)&curProg);
	
	gl.glUseProgram(DebugData.meshProg);
	
	float wireCol[] = { 0.0f, 0.0f, 0.0f, 1.0f };
	GLint colLoc = gl.glGetUniformLocation(DebugData.meshProg, "RENDERDOC_GenericFS_Color");
	gl.glUniform4fv(colLoc, 1, wireCol);
	
	Matrix4f projMat = Matrix4f::Perspective(90.0f, 0.1f, 100000.0f, DebugData.outWidth/DebugData.outHeight);

	Camera cam;
	if(cfg.arcballCamera)
		cam.Arcball(cfg.cameraPos.x, Vec3f(cfg.cameraRot.x, cfg.cameraRot.y, cfg.cameraRot.z));
	else
		cam.fpsLook(Vec3f(cfg.cameraPos.x, cfg.cameraPos.y, cfg.cameraPos.z), Vec3f(cfg.cameraRot.x, cfg.cameraRot.y, cfg.cameraRot.z));

	Matrix4f camMat = cam.GetMatrix();

	Matrix4f ModelViewProj = projMat.Mul(camMat);

	GLint mvpLoc = gl.glGetUniformLocation(DebugData.meshProg, "ModelViewProj");
	gl.glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, ModelViewProj.Data());
	
	GLuint curVAO = 0;
	gl.glGetIntegerv(eGL_VERTEX_ARRAY_BINDING, (GLint*)&curVAO);
	
	GLuint curArr = 0;
	gl.glGetIntegerv(eGL_ARRAY_BUFFER_BINDING, (GLint*)&curArr);

	gl.glBindVertexArray(DebugData.meshVAO);

	// TODO: we should probably use glBindVertexBuffer, glVertexAttribFormat, glVertexAttribBinding.
	// For now just assume things about the format and vbuffer.

	RDCASSERT(attr.Format.compType == eCompType_Float && attr.Format.compByteWidth == 4);

	gl.glBindBuffer(eGL_ARRAY_BUFFER, m_pDriver->GetResourceManager()->GetLiveResource(vb.Buffer).name);
	gl.glVertexAttribPointer(0, attr.Format.compCount, eGL_FLOAT, GL_FALSE, 0, (void *)intptr_t(vb.Offset + attr.RelativeOffset));
	gl.glEnableVertexAttribArray(0);

	{
		GLint depthTest = GL_FALSE;
		gl.glGetIntegerv(eGL_DEPTH_TEST, (GLint*)&depthTest);
		GLenum polyMode = eGL_FILL;
		gl.glGetIntegerv(eGL_POLYGON_MODE, (GLint*)&polyMode);

		gl.glDisable(eGL_DEPTH_TEST);
		gl.glPolygonMode(eGL_FRONT_AND_BACK, eGL_LINE);

		ReplayLog(frameID, 0, eventID[0], eReplay_OnlyDraw);

		if(depthTest)
			gl.glEnable(eGL_DEPTH_TEST);
		if(polyMode != eGL_LINE)
			gl.glPolygonMode(eGL_FRONT_AND_BACK, polyMode);
	}

	gl.glBindVertexArray(curVAO);
	gl.glBindBuffer(eGL_ARRAY_BUFFER, curArr);
	
	gl.glUseProgram(curProg);
	gl.glViewport(viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
	gl.glBindFramebuffer(eGL_FRAMEBUFFER, curFBO);
}
Exemplo n.º 28
0
void MachOObject::loadSegments()
{
	for (Segment* seg : getSegments(*m_file))
	{
		uintptr_t mappingSize;
		int initprot, maxprot;
		void* mappingAddr;
		void* rv;
		int flags = MAP_PRIVATE;
		
		if (strcmp(seg->segname, SEG_PAGEZERO) == 0 || seg->vmsize == 0)
			continue;
		
		assert(seg->vmsize >= seg->filesize);
		
		if (!m_base)
		{
			mappingAddr = (void*) seg->vmaddr;
			
			if (mappingAddr < MachOMgr::instance()->maxAddress())
				mappingAddr = MachOMgr::instance()->maxAddress();
		}
		else
			mappingAddr = (void*) (seg->vmaddr + m_slide);
		
		mappingSize = pageAlign(seg->filesize);
		maxprot = machoProtectionFlagsToMmap(seg->maxprot);
		initprot = machoProtectionFlagsToMmap(seg->initprot);
	
		if (MachOMgr::instance()->printSegments())
			std::cerr << "dyld: Mapping segment " << seg->segname << " from " << m_file->filename() << " to " << mappingAddr << ", slide is 0x" << std::hex << m_slide << std::dec << std::endl;
		
#ifndef __arm__ // All executables on iOS are PIE
		// The first segment can be moved by mmap, but not the following ones
		auto filetype = m_file->header().filetype;
		if (m_base || (!(m_file->header().flags & MH_PIE) && filetype != MH_DYLIB && filetype != MH_BUNDLE))
			flags |= MAP_FIXED;
		else
#endif
		{
			// When letting the system decide where to place the mapping, we need to make sure that the spot chosen
			// is big enough for all segments
			uintptr_t size = getTotalMappingSize();
			rv = ::mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
			if (rv == MAP_FAILED)
			{
				std::stringstream ss;
				ss << "Failed to mmap temporary anonymous range: "  << strerror(errno);
				throw std::runtime_error(ss.str());
			}
			
			flags |= MAP_FIXED;
			mappingAddr = rv;
		}

		rv = ::mmap(mappingAddr, mappingSize, maxprot, flags, m_file->fd(), m_file->offset() + seg->fileoff);
		if (rv == MAP_FAILED)
		{
			std::stringstream ss;
			
			if (errno == EPERM && uintptr_t(mappingAddr) < getMinMappingAddr())
			{
				ss << "This executable is not position independent and your vm.mmap_min_addr is too low to load it. ";
				ss << "As low as " << uintptr_t(mappingAddr) << " is needed.";
			}
			else
				ss << "Failed to mmap '" << m_file->filename() << "': " << strerror(errno);
			throw std::runtime_error(ss.str());
		}
		if (!m_base)
		{
			m_slide = (intptr_t(rv) - intptr_t(seg->vmaddr));
			m_base = rv;
			mappingAddr = rv;
		}
		
		m_mappings.push_back(Mapping { mappingAddr, pageAlign(seg->vmsize), initprot, maxprot });

		if (seg->vmsize > mappingSize)
		{
			// Map empty pages to cover the vmsize range
			mappingAddr = (void*) (seg->vmaddr + m_slide + mappingSize);
			mappingSize = seg->vmsize - mappingSize;
			
			rv = ::mmap(mappingAddr, mappingSize, maxprot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
			//int err = ::mprotect(mappingAddr, mappingSize, maxprot);
			
			if (rv == MAP_FAILED)
			{
				std::stringstream ss;
				ss << "Failed to mmap anonymous pages for '" << m_file->filename() << "': " << strerror(errno);
				throw std::runtime_error(ss.str());
			}
		}
	}
}
Exemplo n.º 29
0
 bool operator()(const char* a, const char* b) {
   return intptr_t(a) > 0 && (strcmp(a, b) == 0);
 }
Exemplo n.º 30
0
void Stats::FinishedSingleOp(OP_TYPE type, const timespec& beg, const timespec& end) {
  opsDataCq[intptr_t(type)].push(std::make_pair(
      1000000000LL * beg.tv_sec + beg.tv_nsec,
      1000000000LL * end.tv_sec + end.tv_nsec)
      );
}